1 / 24

SharePoint Object Model

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.

zizi
Download Presentation

SharePoint Object Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SharePoint Object Model

  2. 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

  3. 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

  4. 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!

  5. ObjectmodelofServerArchitecture

  6. Object model of Site Architecture Content Management

  7. 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

  8. 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.

  9. 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; …

  10. 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);

  11. 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;

  12. 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()); }

  13. 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>"); }

  14. 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();

  15. 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();

  16. 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>"); } } } }

  17. 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

  18. 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);

  19. 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.

  20. 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

  21. DEMO Creating Site Collection

  22. Q&A

  23. Resources

  24. Resources

More Related