sharepoint object model
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 PresentationTRANSCRIPT
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
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
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
Object model of Server Architecture
ContentManagemen
t
Object model of Site Object model of Site ArchitectureArchitecture
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
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.
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;
……
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);
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;
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());
}}
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>");}}
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();
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();
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>");}}
}}
}}}}
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
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);
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.
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
DEMO Creating Site Collection
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!
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