sharepoint object model

24

Upload: zizi

Post on 20-Jan-2016

54 views

Category:

Documents


1 download

DESCRIPTION

SharePoint Object Model. Agenda. Introduction to the Object Model Object model of Server Architecture Object model of Site Architecture Windows SharePoint Services Objects Accessing data in a WSS List Updating data Tips & Tricks SharePoint Web Services Working with Lists. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SharePoint Object Model
Page 2: SharePoint Object Model

AgendaAgenda

Introduction to the Object ModelIntroduction to the Object Model Object model of Server ArchitectureObject model of Server Architecture Object model of Site ArchitectureObject model of Site Architecture Windows SharePoint Services ObjectsWindows SharePoint Services Objects Accessing data in a WSS ListAccessing data in a WSS List Updating dataUpdating data Tips & TricksTips & Tricks SharePoint Web ServicesSharePoint Web Services Working with ListsWorking with Lists

Page 3: SharePoint Object Model

Managed code object model on the server Accessible via ASP.NET or any other

server process Implemented in C# Exposes almost of all of the data stored in

WSS

Introduction to the Object Model

Page 4: SharePoint Object Model

Examples of what can be done with the Object Mode:

Add, edit, delete, and retrieve data from SharePoint Lists Create new lists and set list metadata (e.g. the fields in a list) Work with documents in document libraries. Perform administrative tasks such as creating webs, adding users, creating roles, etc.

Pretty much any functionality in the UI can be automated through the OM!

Introduction to the Object Model

Page 5: SharePoint Object Model

Object model of Server Architecture

Page 6: SharePoint Object Model

ContentManagemen

t

Object model of Site Object model of Site ArchitectureArchitecture

Page 7: SharePoint Object Model

Windows SharePoint Services Windows SharePoint Services ObjectsObjects

ListsLists SPListSPList SPListCollectionSPListCollection SPListItemSPListItem SPListItemCollectionSPListItemCollection SPViewSPView SPFieldSPField SPListTemplateSPListTemplate

FilesFiles SPFileSPFile SPFileCollectionSPFileCollection SPFileVersionSPFileVersion SPFolderSPFolder SPDocumentLibrarySPDocumentLibrary SPDocDiscussionSPDocDiscussion SPDocTemplateSPDocTemplate

ListsLists SPListSPList SPListCollectionSPListCollection SPListItemSPListItem SPListItemCollectiSPListItemCollecti

onon SPViewSPView SPFieldSPField SPListTemplateSPListTemplate

FilesFiles SPFileSPFile SPFileCollectionSPFileCollection SPFileVersionSPFileVersion SPFolderSPFolder SPDocumentLibrarSPDocumentLibrar

yy SPDocDiscussionSPDocDiscussion SPDocTemplateSPDocTemplate

ListsLists SPListSPList SPListCollectionSPListCollection SPListItemSPListItem SPListItemCollectionSPListItemCollection SPViewSPView SPFieldSPField SPListTemplateSPListTemplate

FilesFiles SPFileSPFile SPFileCollectionSPFileCollection SPFileVersionSPFileVersion SPFolderSPFolder SPDocumentLibrarySPDocumentLibrary SPDocDiscussionSPDocDiscussion SPDocTemplateSPDocTemplate

SecuritySecurity SPUserSPUser SPRoleSPRole SPGroupSPGroup SPPermissionSPPermission SPRightsEnumeratioSPRightsEnumeratio

nn

AdministrationAdministration SPGlobalAdminSPGlobalAdmin SPVirtualServerSPVirtualServer SPQuotaSPQuota SPGlobalConfigSPGlobalConfig SPSiteCollectionSPSiteCollection

DocumentsDocuments SPDocumentLibrarSPDocumentLibrar

yy SPFileSPFile SPFileCollectionSPFileCollection SPFolderSPFolder

Page 8: SharePoint Object Model

Working with the OMWorking with the OM The object model has three top-level The object model has three top-level

objects:objects: SPWeb (represents an individual site)SPWeb (represents an individual site) SPSite (represents a site collection, which is a SPSite (represents a site collection, which is a

set of web sites)set of web sites) SPGlobalAdmin (used for global administration SPGlobalAdmin (used for global administration

settings)settings) In order to perform actions on data within In order to perform actions on data within

a web, you must first get an SPWeb object.a web, you must first get an SPWeb object.

Page 9: SharePoint Object Model

Adding our namespaceAdding our namespace You should add references to the You should add references to the

WSS namespaces to your source WSS namespaces to your source filesfilesusing Microsoft.SharePoint;using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.Administration;using Microsoft.SharePoint.Administration;

……

Page 10: SharePoint Object Model

Key Object – SPWebKey Object – SPWeb Starting point to get at the Lists, Items, Starting point to get at the Lists, Items,

Documents, Users, Alerts, etc. for a web site.Documents, Users, Alerts, etc. for a web site. Example Properties:Example Properties:

Web.Lists (returns a collection of lists)Web.Lists (returns a collection of lists) Web.Title (returns the title of the site)Web.Title (returns the title of the site) Web.Users (returns the users on the site)Web.Users (returns the users on the site)

In a web part or ASPX page, you can use the In a web part or ASPX page, you can use the following line to get a SPWeb:following line to get a SPWeb:SPWeb myweb = SPControl.GetContextWeb(Context);SPWeb myweb = SPControl.GetContextWeb(Context);

Page 11: SharePoint Object Model

Accessing data in a WSS Accessing data in a WSS ListList

Get a SPList or SPDocumentLibrary object.Get a SPList or SPDocumentLibrary object.SPList mylist = web.Lists[“Events”];SPList mylist = web.Lists[“Events”];

You can then call the .Items property to You can then call the .Items property to get all of the items:get all of the items:SPListItemCollection items = mylist.Items;SPListItemCollection items = mylist.Items;

Page 12: SharePoint Object Model

Accessing data in a listAccessing data in a list To get data for a field, specify the field To get data for a field, specify the field

name in the indexer for an SPListItemname in the indexer for an SPListItemforeach(SPListItem item in items)foreach(SPListItem item in items)

{{

Response.Write(item["Due Date"].ToString());Response.Write(item["Due Date"].ToString());

Response.Write(item["Status"].ToString());Response.Write(item["Status"].ToString());

Response.WRite(item["Title"].ToString());Response.WRite(item["Title"].ToString());

}}

Page 13: SharePoint Object Model

Full ExampleFull ExampleSPWeb web = SPControl.GetContextWeb(Context);SPWeb web = SPControl.GetContextWeb(Context);

SPList tasks = web.Lists["Tasks"];SPList tasks = web.Lists["Tasks"];SPListItemCollection items=tasks.Items;SPListItemCollection items=tasks.Items;foreach(SPListItem item in items)foreach(SPListItem item in items){{output.Write(item["Title"].ToString() + output.Write(item["Title"].ToString() + item["Status"].ToString() + "<br>");item["Status"].ToString() + "<br>");}}

Page 14: SharePoint Object Model

Updating dataUpdating data Most objects in WSS do not immediately Most objects in WSS do not immediately

update data when you change a propertyupdate data when you change a property You need to first call the Update() method You need to first call the Update() method

on the objecton the object This helps performance by minimizing SQL This helps performance by minimizing SQL

queries underneath the coversqueries underneath the covers Example:Example:

SPList mylist = web.Lists[“Tasks”];SPList mylist = web.Lists[“Tasks”];

mylist.Title=“Tasks!!!”;mylist.Title=“Tasks!!!”;

mylist.Description=“Description!!”;mylist.Description=“Description!!”;

Mylist.Update();Mylist.Update();

Page 15: SharePoint Object Model

Updating List DataUpdating List Data SPListItem is another example of an SPListItem is another example of an

object where you need to call update:object where you need to call update: Example:Example:

SPListItem item = items[0];SPListItem item = items[0];

item["Status"]="Not Started";item["Status"]="Not Started";

item["Title"]="Task Title";item["Title"]="Task Title";

item.Update();item.Update();

Page 16: SharePoint Object Model

Code Example -- Enumerate Lists Code Example -- Enumerate Lists and Websand Webs

private void ShowSubWebs(HtmlTextWriter output)private void ShowSubWebs(HtmlTextWriter output){{

SPWeb web = SPControl.GetContextWeb(Context);SPWeb web = SPControl.GetContextWeb(Context);SPWebCollection mywebs = web.Webs;SPWebCollection mywebs = web.Webs;foreach (SPWeb myweb in mywebs)foreach (SPWeb myweb in mywebs){{

output.Write(myweb.Title + "<br>");output.Write(myweb.Title + "<br>");}}

}}

private void ShowSubWebsWithLists(HtmlTextWriter output)private void ShowSubWebsWithLists(HtmlTextWriter output){{

SPWeb web = SPControl.GetContextWeb(Context);SPWeb web = SPControl.GetContextWeb(Context);SPWebCollection mywebs = web.Webs;SPWebCollection mywebs = web.Webs;foreach (SPWeb myweb in mywebs)foreach (SPWeb myweb in mywebs){{

output.Write("<b>" + myweb.Title + "<br>" + "</b>");output.Write("<b>" + myweb.Title + "<br>" + "</b>");SPListCollection lists = myweb.Lists;SPListCollection lists = myweb.Lists;foreach (SPList list in lists)foreach (SPList list in lists){{

if (list.ItemCount>10)if (list.ItemCount>10){{output.Write(list.Title + ": " + list.ItemCount + output.Write(list.Title + ": " + list.ItemCount +

"<br>");"<br>");}}

}}

}}}}

Page 17: SharePoint Object Model

Tips & TricksTips & Tricks

To optimize performance, use foreach() to step To optimize performance, use foreach() to step through collections. Iterating through collections through collections. Iterating through collections by index can result in unnecessary database calls by index can result in unnecessary database calls

Calls to collections such as List.Items are Calls to collections such as List.Items are expensive. Preserve the collection rather than expensive. Preserve the collection rather than requesting it againrequesting it again

For best performance, use SQL Profiler to For best performance, use SQL Profiler to minimize the # of queries that your app makes to minimize the # of queries that your app makes to the databasethe database

Page 18: SharePoint Object Model

Tips & TricksTips & Tricks User InterfaceUser Interface -> -> OMOM terminology mapping: terminology mapping:

Site CollectionSite Collection -> -> SiteSite SiteSite -> -> WebWeb Top-level SiteTop-level Site -> -> RootwebRootweb SubsiteSubsite -> -> SubwebSubweb

Free your objects when you’re done using Free your objects when you’re done using them. Call ‘Close’ or ‘Dispose’ on Web and them. Call ‘Close’ or ‘Dispose’ on Web and Site objects.Site objects.

Use the following command to get the current Use the following command to get the current SPWeb object from a web part or aspx page:SPWeb object from a web part or aspx page:SPWeb web = SPControl.GetContextWeb(Context); SPWeb web = SPControl.GetContextWeb(Context);

Page 19: SharePoint Object Model

SharePoint Web ServicesSharePoint Web ServicesSharePoint has web services APIs for accessing

content remotely. The web services layer are built on top of the server OM.

Allows manipulation of Lists, Webs, Views, List Items, etc.

Functionality will be similar to server object model, but with fewer interfaces optimized to minimize transactions.

Microsoft Office 2003 (e.g. Excel, Data Sheet, Work, Outlook, FrontPage, etc) use web services to access data from SharePoint Services.

Page 20: SharePoint Object Model

SharePoint Web ServicesSharePoint Web Services

Create a Windows ApplicationIn Visual Studio.Net, choose ‘Add Web Reference’

Enter http://server/_vti_bin/lists.asmx to access the lists web serviceOther services include:

Webs.asmx – Web informationViews.asmx – View informationAlerts.asmx – AlertsAdmin.asmx – Administering SitesPermissions.asmx, UserGroups.asmx – Site permissionsVersions.asmx – File Version InfoForms.asmx – Form information

Page 21: SharePoint Object Model

DEMO Creating Site Collection

Page 22: SharePoint Object Model
Page 23: SharePoint Object Model

ResourcesResources1.1. Fantastic “40” and Splendid “7” – Fantastic “40” and Splendid “7” – http://www.microsoft.com/office/sharepoint

2.2. SharePoint Team blog – SharePoint Team blog – http://msdn.microsoft.com/sharepoint

3.3. My new book from Wrox of course! Pro SharePoint DevelopmentMy new book from Wrox of course! Pro SharePoint Development

4.4.Telerik’s RAD Editor for SharePoint (Free!)Telerik’s RAD Editor for SharePoint (Free!)

http://www.telerik.com/products/sharepoint/overview.aspx http://www.telerik.com/products/sharepoint/overview.aspx

5.5. MOSS SDK – http://msdn.microsoft.com/MOSSMOSS SDK – http://msdn.microsoft.com/MOSS

6.6.Codeplex – http://www.codeplex.com Some favorites – SmartpartCodeplex – http://www.codeplex.com Some favorites – Smartpart

Sharepoint Inspector, CAML ViewerSharepoint Inspector, CAML Viewer

7.7. Dan Winter’s Blog - http://blogs.msdn.com/dwinterDan Winter’s Blog - http://blogs.msdn.com/dwinter

8.8. SharePoint Designer Free Training!!!! SharePoint Designer Free Training!!!! http://office.microsoft.com/en-us/help/HA102199841033.aspx http://office.microsoft.com/en-us/help/HA102199841033.aspx

9.9. JoelO’s SharePointLand (Tons of admin content in Joel’s style!) – JoelO’s SharePointLand (Tons of admin content in Joel’s style!) – http://blogs.msdn.com/joelohttp://blogs.msdn.com/joelo

10.10. All the SharePoint bloggers! Thank you all!All the SharePoint bloggers! Thank you all!

Page 24: SharePoint Object Model

ResourcesResources

1.1.Technical Chats and Webcastshttp://www.microsoft.com/usa/webcasts/default.asp

2.2.Microsoft Learning and Certificationhttp://www.microsoft.com/learning/default.mspx

3.3.MSDN & TechNet http://microsoft.com/msdnhttp://microsoft.com/technet

4.4.Technical Community Siteshttp://www.microsoft.com/communities/default.mspx