250 likes | 460 Views
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.
E N D
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
Introduction to the 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 • 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!
Object model of Site Architecture Content Management
Windows SharePoint Services Objects • Security • SPUser • SPRole • SPGroup • SPPermission • SPRightsEnumeration • Administration • SPGlobalAdmin • SPVirtualServer • SPQuota • SPGlobalConfig • SPSiteCollection • Documents • SPDocumentLibrary • SPFile • SPFileCollection • SPFolder • Lists • SPList • SPListCollection • SPListItem • SPListItemCollection • SPView • SPField • SPListTemplate • Files • SPFile • SPFileCollection • SPFileVersion • SPFolder • SPDocumentLibrary • SPDocDiscussion • SPDocTemplate • Lists • SPList • SPListCollection • SPListItem • SPListItemCollection • SPView • SPField • SPListTemplate • Files • SPFile • SPFileCollection • SPFileVersion • SPFolder • SPDocumentLibrary • SPDocDiscussion • SPDocTemplate • Lists • SPList • SPListCollection • SPListItem • SPListItemCollection • SPView • SPField • SPListTemplate • Files • SPFile • SPFileCollection • SPFileVersion • SPFolder • SPDocumentLibrary • SPDocDiscussion • SPDocTemplate
Working with the OM • The object model has three top-level objects: • SPWeb (represents an individual site) • SPSite (represents a site collection, which is a set of web sites) • SPGlobalAdmin (used for global administration settings) • In order to perform actions on data within a web, you must first get an SPWeb object.
Adding our namespace • You should add references to the WSS namespaces to your source files using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.Administration; …
Key Object – SPWeb • Starting point to get at the Lists, Items, Documents, Users, Alerts, etc. for a web site. • Example Properties: • Web.Lists (returns a collection of lists) • Web.Title (returns the title of the site) • Web.Users (returns the users on the site) • In a web part or ASPX page, you can use the following line to get a SPWeb: SPWeb myweb = SPControl.GetContextWeb(Context);
Accessing data in a WSS List • Get a SPList or SPDocumentLibrary object. SPListmylist = web.Lists[“Events”]; • You can then call the .Items property to get all of the items: SPListItemCollection items = mylist.Items;
Accessing data in a list • To get data for a field, specify the field name in the indexer for an SPListItem foreach(SPListItem item in items) { Response.Write(item["Due Date"].ToString()); Response.Write(item["Status"].ToString()); Response.WRite(item["Title"].ToString()); }
Full Example SPWeb web = SPControl.GetContextWeb(Context); SPList tasks = web.Lists["Tasks"]; SPListItemCollection items=tasks.Items; foreach(SPListItem item in items) { output.Write(item["Title"].ToString() + item["Status"].ToString() + "<br>"); }
Updating data • Most objects in WSS do not immediately update data when you change a property • You need to first call the Update() method on the object • This helps performance by minimizing SQL queries underneath the covers • Example: SPListmylist = web.Lists[“Tasks”]; mylist.Title=“Tasks!!!”; mylist.Description=“Description!!”; Mylist.Update();
Updating List Data • SPListItem is another example of an object where you need to call update: • Example: SPListItem item = items[0]; item["Status"]="Not Started"; item["Title"]="Task Title"; item.Update();
Code Example -- Enumerate Lists and Webs private void ShowSubWebs(HtmlTextWriter output) { SPWeb web = SPControl.GetContextWeb(Context); SPWebCollection mywebs = web.Webs; foreach (SPWeb myweb in mywebs) { output.Write(myweb.Title + "<br>"); } } private void ShowSubWebsWithLists(HtmlTextWriter output) { SPWeb web = SPControl.GetContextWeb(Context); SPWebCollection mywebs = web.Webs; foreach (SPWeb myweb in mywebs) { output.Write("<b>" + myweb.Title + "<br>" + "</b>"); SPListCollection lists = myweb.Lists; foreach (SPList list in lists) { if (list.ItemCount>10) { output.Write(list.Title + ": " + list.ItemCount + "<br>"); } } } }
Tips & Tricks • To optimize performance, use foreach() to step through collections. Iterating through collections by index can result in unnecessary database calls • Calls to collections such as List.Items are expensive. Preserve the collection rather than requesting it again • For best performance, use SQL Profiler to minimize the # of queries that your app makes to the database
Tips & Tricks • User Interface -> OM terminology mapping: • Site Collection -> Site • Site -> Web • Top-level Site -> Rootweb • Subsite -> Subweb • Free your objects when you’re done using them. Call ‘Close’ or ‘Dispose’ on Web and Site objects. • Use the following command to get the current SPWeb object from a web part or aspx page: SPWeb web = SPControl.GetContextWeb(Context);
SharePoint Web Services • SharePoint 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 Services • Create a Windows Application • In Visual Studio.Net, choose ‘Add Web Reference’ • Enter http://server/_vti_bin/lists.asmx to access the lists web service • Other services include: • Webs.asmx – Web information • Views.asmx – View information • Alerts.asmx – Alerts • Admin.asmx – Administering Sites • Permissions.asmx, UserGroups.asmx – Site permissions • Versions.asmx – File Version Info • Forms.asmx – Form information
DEMO Creating Site Collection