Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Programming SharePoint Object Model, Web Services, and Events Michael Morton 4/15/03
Summary of .NET support <ul><li>SharePoint will use ASP.NET instead of ISAPI for base page execution </li></ul><ul><li>Web...
.NET Object Model <ul><li>Managed code object model on the server </li></ul><ul><li>Accessible via ASP.NET or any other se...
NET Object model <ul><li>Examples of what can be done with the Object Mode: </li></ul><ul><ul><li>Add, edit, delete, and r...
Example Objects <ul><li>List Data </li></ul><ul><ul><li>SPField </li></ul></ul><ul><ul><li>SPFieldCollection </li></ul></u...
ASP.Net Security <ul><li>For content stored in WSS, only registered set of web custom controls will run in pages </li></ul...
Getting Started with OM <ul><li>Build a web part  </li></ul><ul><ul><li>This is the best option to write code that functio...
Getting Started with the OM <ul><li>Build an ASPX page </li></ul><ul><ul><li>Code cannot live inline in a page within the ...
Getting Started with the OM <ul><li>Windows Executable or any other application </li></ul><ul><ul><li>Object model can be ...
Demo <ul><li>Hello World Web Part </li></ul>
Working with the OM <ul><li>The object model has three top-level objects: </li></ul><ul><ul><li>SPWeb (represents an indiv...
Adding our namespace <ul><li>You should add references to the WSS namespaces to your source files </li></ul><ul><ul><li>us...
Key Object – SPWeb <ul><li>Starting point to get at the Lists, Items, Documents, Users, Alerts, etc. for a web site. </li>...
Demo <ul><li>Showing Web and List Properties </li></ul>
Accessing data in a WSS List <ul><li>Get a SPList or SPDocumentLibrary object. </li></ul><ul><ul><li>SPList mylist = web.L...
Accessing data in a list <ul><li>To get data for a field, specify the field name in the indexer for an SPListItem </li></u...
Full Example <ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul><ul><li>SPList tasks = web.Lists[&quot;Tasks&...
Updating data <ul><li>Most objects in WSS do not immediately update data when you change a property </li></ul><ul><li>You ...
Updating List Data <ul><li>SPListItem is another example of an object where you need to call update: </li></ul><ul><li>Exa...
FormDigest Security <ul><li>By default, the object model will not allow data updates if the form submitting the data does ...
Adding Users to a web <ul><li>Get the appropriate SPRole object: </li></ul><ul><li>SPRole admins = web.Roles[&quot;Adminis...
Demo <ul><li>Adding users to the site via the OM </li></ul>
Keep objects around <ul><li>If you create and destroy objects frequently, you may do extra SQL queries and have code that ...
<ul><li>SharePoint will have web services APIs for accessing content.  The web services layer will be built on top of the ...
Web Service Methods <ul><li>GetListCollection </li></ul><ul><li>GetListItems </li></ul><ul><li>GetWebCollection </li></ul>...
Getting Started With Web Services <ul><li>Create a Windows Application </li></ul><ul><li>In Visual Studio, choose ‘Add Web...
Getting Started with Web Services <ul><li>To send the logged on users’ credentials from the client, add the following line...
Demo <ul><li>Building a Web Service Client </li></ul>
Events <ul><li>We support events on document libraries. </li></ul><ul><ul><li>Operations such as add, update, delete, chec...
Optimizing Performance of OM <ul><li>The biggest goal is to minimize the number of SQL queries.  </li></ul><ul><ul><li>It ...
What about CAML? <ul><li>Page Execution will no longer be driven by CAML (XML schema used in SharePoint) </li></ul><ul><li...
SDK Available <ul><li>Documentation about V2 available at  http://msdn.microsoft.com/sharepoint/ </li></ul>
Questions?
 
Code Example -- Enumerate Lists and Webs <ul><li>private void ShowSubWebs(HtmlTextWriter output) </li></ul><ul><li>{ </li>...
Code Snippet – Copy Files <ul><li>private SPWeb web; </li></ul><ul><li>private void Page_Load(object sender, System.EventA...
Code Snippet – Add users <ul><li>private void Button1_Click(object sender, System.EventArgs e) </li></ul><ul><li>{ </li></...
Upcoming SlideShare
Loading in …5
×

Wss Object Model

8,206 views

Published on

Published in: Technology
  • Be the first to comment

Wss Object Model

  1. 1. Programming SharePoint Object Model, Web Services, and Events Michael Morton 4/15/03
  2. 2. Summary of .NET support <ul><li>SharePoint will use ASP.NET instead of ISAPI for base page execution </li></ul><ul><li>Web Part Framework </li></ul><ul><li>Server Object Model for programmatic access to SharePoint data </li></ul><ul><li>We offer functionality as XML web services for access from remote machines </li></ul>
  3. 3. .NET Object Model <ul><li>Managed code object model on the server </li></ul><ul><li>Accessible via ASP.NET or any other server process </li></ul><ul><li>Implemented in C# </li></ul><ul><li>Exposes almost of all of the data stored in WSS </li></ul>
  4. 4. NET Object model <ul><li>Examples of what can be done with the Object Mode: </li></ul><ul><ul><li>Add, edit, delete, and retrieve data from SharePoint Lists </li></ul></ul><ul><ul><li>Create new lists and set list metadata (e.g. the fields in a list) </li></ul></ul><ul><ul><li>Set web properties </li></ul></ul><ul><ul><li>Work with documents in document libraries. </li></ul></ul><ul><ul><li>Perform administrative tasks such as creating webs, adding users, creating roles, etc. </li></ul></ul><ul><ul><li>Pretty much any functionality in the UI can be automated through the OM! </li></ul></ul>
  5. 5. Example Objects <ul><li>List Data </li></ul><ul><ul><li>SPField </li></ul></ul><ul><ul><li>SPFieldCollection </li></ul></ul><ul><ul><li>SPListCollection </li></ul></ul><ul><ul><li>SPList </li></ul></ul><ul><ul><li>SPListItemCollection </li></ul></ul><ul><ul><li>SPListItem </li></ul></ul><ul><ul><li>SPView </li></ul></ul><ul><li>Administration </li></ul><ul><ul><li>SPGlobalAdmin </li></ul></ul><ul><ul><li>SPQuota </li></ul></ul><ul><ul><li>SPVirtualServer </li></ul></ul><ul><li>Security </li></ul><ul><ul><li>SPGroup </li></ul></ul><ul><ul><li>SPGroupCollection </li></ul></ul><ul><ul><li>SPSite </li></ul></ul><ul><ul><li>SPUser </li></ul></ul><ul><ul><li>SPUserCollection </li></ul></ul><ul><li>Documents </li></ul><ul><ul><li>SPDocumentLibrary </li></ul></ul><ul><ul><li>SPFile </li></ul></ul><ul><ul><li>SPFileCollection </li></ul></ul><ul><ul><li>SPFolder </li></ul></ul>
  6. 6. ASP.Net Security <ul><li>For content stored in WSS, only registered set of web custom controls will run in pages </li></ul><ul><li>Inline script in the page will not execute </li></ul><ul><ul><li>Code behind in pages can be made to work </li></ul></ul><ul><li>All Executable code (e.g. web custom controls, web parts, and code-behind classes) needs to be installed on physical web server </li></ul>
  7. 7. Getting Started with OM <ul><li>Build a web part </li></ul><ul><ul><li>This is the best option to write code that functions are part of a WSS site or solution </li></ul></ul><ul><ul><li>There will be lots of documentation with the beta on how to build a web part. </li></ul></ul><ul><ul><li>Web Part is reusable and can be managed using all of the web part tools and UI. </li></ul></ul>
  8. 8. Getting Started with the OM <ul><li>Build an ASPX page </li></ul><ul><ul><li>Code cannot live inline in a page within the site. </li></ul></ul><ul><ul><li>Creating pages underneath the /_layouts directory is often the best option for custom ASPX apps on top of SharePoint </li></ul></ul><ul><ul><ul><li>This lets your page be accessible from any web. For example, if you build mypage.aspx in _Layouts, it is accessible from the following URLs: </li></ul></ul></ul><ul><ul><ul><ul><li>http://myweb/_layouts/myapp/mypage.aspx </li></ul></ul></ul></ul><ul><ul><ul><ul><li>http://myweb/subweb1/_layouts/myapp/mypage.aspx </li></ul></ul></ul></ul><ul><ul><ul><li>ASPX page will run using the context of the web under which it is running. </li></ul></ul></ul>
  9. 9. Getting Started with the OM <ul><li>Windows Executable or any other application </li></ul><ul><ul><li>Object model can be called from pretty much any code context. It is not limited to just web parts or ASP.Net </li></ul></ul><ul><ul><li>For example, you could build a command-line utility to perform certain actions </li></ul></ul>
  10. 10. Demo <ul><li>Hello World Web Part </li></ul>
  11. 11. Working with the OM <ul><li>The object model has three top-level objects: </li></ul><ul><ul><li>SPWeb (represents an individual site) </li></ul></ul><ul><ul><li>SPSite (represents a site collection, which is a set of web sites) </li></ul></ul><ul><ul><li>SPGlobalAdmin (used for global administration settings) </li></ul></ul><ul><li>In order to perform actions on data within a web, you must first get an SPWeb object. </li></ul>
  12. 12. Adding our namespace <ul><li>You should add references to the WSS namespaces to your source files </li></ul><ul><ul><li>using Microsoft.SharePoint; </li></ul></ul><ul><ul><li>using Microsoft.SharePoint.WebControls; </li></ul></ul><ul><ul><li>using Microsoft.SharePoint.Administration; </li></ul></ul><ul><ul><li>… </li></ul></ul>
  13. 13. Key Object – SPWeb <ul><li>Starting point to get at the Lists, Items, Documents, Users, Alerts, etc. for a web site. </li></ul><ul><li>Example Properties: </li></ul><ul><ul><li>Web.Lists (returns a collection of lists) </li></ul></ul><ul><ul><li>Web.Title (returns the title of the site) </li></ul></ul><ul><ul><li>Web.Users (returns the users on the site) </li></ul></ul><ul><li>In a web part or ASPX page, you can use the following line to get a SPWeb: </li></ul><ul><ul><li>SPWeb myweb = SPControl.GetContextWeb(Context); </li></ul></ul>
  14. 14. Demo <ul><li>Showing Web and List Properties </li></ul>
  15. 15. Accessing data in a WSS List <ul><li>Get a SPList or SPDocumentLibrary object. </li></ul><ul><ul><li>SPList mylist = web.Lists[“Events”]; </li></ul></ul><ul><li>You can then call the .Items property to get all of the items: </li></ul><ul><ul><li>SPListItemCollection items = mylist.Items; </li></ul></ul><ul><li>If you only want a subset of the items, call the GetItems method and pass a SPQuery object </li></ul><ul><ul><li>SPListItemCollection items = mylist.GetItems(query); </li></ul></ul>
  16. 16. Accessing data in a list <ul><li>To get data for a field, specify the field name in the indexer for an SPListItem </li></ul><ul><ul><li>foreach(SPListItem item in items) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>Response.Write(item[&quot;Due Date&quot;].ToString()); </li></ul></ul><ul><ul><li>Response.Write(item[&quot;Status&quot;].ToString()); </li></ul></ul><ul><ul><li>Response.WRite(item[&quot;Title&quot;].ToString()); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  17. 17. Full Example <ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul><ul><li>SPList tasks = web.Lists[&quot;Tasks&quot;]; </li></ul><ul><li>SPListItemCollection items=tasks.Items; </li></ul><ul><li>foreach(SPListItem item in items) </li></ul><ul><li>{ </li></ul><ul><li>output.Write(item[&quot;Title&quot;].ToString() + item[&quot;Status&quot;].ToString() + &quot;<br>&quot;); </li></ul><ul><li>} </li></ul>
  18. 18. Updating data <ul><li>Most objects in WSS do not immediately update data when you change a property </li></ul><ul><li>You need to first call the Update() method on the object </li></ul><ul><ul><li>This helps performance by minimizing SQL queries underneath the covers </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li>SPList mylist = web.Lists[“Tasks”]; </li></ul></ul><ul><ul><li>mylist.Title=“Tasks!!!”; </li></ul></ul><ul><ul><li>mylist.Description=“Description!!”; </li></ul></ul><ul><ul><li>Mylist.Update(); </li></ul></ul>
  19. 19. Updating List Data <ul><li>SPListItem is another example of an object where you need to call update: </li></ul><ul><li>Example: </li></ul><ul><ul><li>SPListItem item = items[0]; </li></ul></ul><ul><ul><li>item[&quot;Status&quot;]=&quot;Not Started&quot;; </li></ul></ul><ul><ul><li>item[&quot;Title&quot;]=&quot;Task Title&quot;; </li></ul></ul><ul><ul><li>item.Update(); </li></ul></ul>
  20. 20. FormDigest Security <ul><li>By default, the object model will not allow data updates if the form submitting the data does not contain the ‘FormDigest’ security key. </li></ul><ul><li>FormDigest is based on username and site. It will time out after 30 minutes. </li></ul><ul><li>Best solution is to include <FormDigest runat=“Server”/> web folder control in ASPX page. </li></ul><ul><li>If you do not need the security the FormDigest provides, you can set to SPWeb.AllowUnsafeUpdates to bypass this check. </li></ul>
  21. 21. Adding Users to a web <ul><li>Get the appropriate SPRole object: </li></ul><ul><li>SPRole admins = web.Roles[&quot;Administrator&quot;]; </li></ul><ul><li>Call the AddUser method: </li></ul><ul><li>admins.AddUser(&quot;redmondfoltz&quot;,&quot;Greg@hotmail.com&quot;,&quot;Greg Foltz&quot;,&quot;&quot;); </li></ul>
  22. 22. Demo <ul><li>Adding users to the site via the OM </li></ul>
  23. 23. Keep objects around <ul><li>If you create and destroy objects frequently, you may do extra SQL queries and have code that is incorrect: </li></ul><ul><li>Bad Example: </li></ul><ul><ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul></ul><ul><ul><li>web.Lists[&quot;Tasks&quot;].Title=&quot;mytitle&quot;; </li></ul></ul><ul><ul><li>web.Lists[&quot;Tasks&quot;].Description=&quot;mydescription&quot;; </li></ul></ul><ul><ul><li>web.Lists[&quot;Tasks&quot;].Update(); </li></ul></ul><ul><li>Good Example: </li></ul><ul><ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul></ul><ul><ul><li>SPList mylist = web.Lists[&quot;Tasks&quot;]; </li></ul></ul><ul><ul><li>mylist.Title=&quot;mytitle&quot;; </li></ul></ul><ul><ul><li>mylist.Description=&quot;mydescription&quot;; </li></ul></ul><ul><ul><li>mylist.Update(); </li></ul></ul>
  24. 24. <ul><li>SharePoint will have web services APIs for accessing content. The web services layer will be built on top of the server OM. </li></ul><ul><li>Allows manipulation of Lists, Webs, Views, List Items, etc. </li></ul><ul><li>Functionality will be similar to server object model, but with fewer interfaces optimized to minimize transactions. </li></ul><ul><li>Office11 (e.g. Excel, DataSheet, Work, Outlook, FrontPage, etc) use web services to access data from WSS. </li></ul>Web Services in WSS
  25. 25. Web Service Methods <ul><li>GetListCollection </li></ul><ul><li>GetListItems </li></ul><ul><li>GetWebCollection </li></ul><ul><li>UpdateList </li></ul><ul><li>UpdateListItems </li></ul><ul><li>GetWebInfo </li></ul><ul><li>GetWebPart </li></ul><ul><li>GetSmartPageDocument </li></ul><ul><li>And more… </li></ul>
  26. 26. Getting Started With Web Services <ul><li>Create a Windows Application </li></ul><ul><li>In Visual Studio, choose ‘Add Web Reference’ </li></ul><ul><ul><li>Enter http://<server>/_vti_bin/lists.asmx to access the lists web service </li></ul></ul><ul><ul><li>Other services include: </li></ul></ul><ul><ul><ul><li>UserGroups.asmx – users and groups </li></ul></ul></ul><ul><ul><ul><li>Webs.asmx – Web information </li></ul></ul></ul><ul><ul><ul><li>Views.asmx – View information </li></ul></ul></ul><ul><ul><ul><li>Subscription.asmx – Subscriptions </li></ul></ul></ul>
  27. 27. Getting Started with Web Services <ul><li>To send the logged on users’ credentials from the client, add the following line in the web reference object’s constructor: </li></ul><ul><ul><li>public Lists() { </li></ul></ul><ul><ul><li>this.Url = &quot;http://mikmort3/_vti_bin/lists.asmx&quot;; </li></ul></ul><ul><ul><li>this.Credentials=System.Net.CredentialCache.DefaultCredentials; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  28. 28. Demo <ul><li>Building a Web Service Client </li></ul>
  29. 29. Events <ul><li>We support events on document libraries. </li></ul><ul><ul><li>Operations such as add, update, delete, check-in, check-out, etc. </li></ul></ul><ul><ul><li>Events are asynchronous </li></ul></ul><ul><ul><li>Events call IListEventSink managed interface. </li></ul></ul><ul><li>Documentation and Sample in the SDK </li></ul>
  30. 30. Optimizing Performance of OM <ul><li>The biggest goal is to minimize the number of SQL queries. </li></ul><ul><ul><li>It may be helpful to use the SQL profiler to monitor what the OM is doing underneath the covers </li></ul></ul><ul><li>Minimizing managed/unmanaged transitions also a goal, though this is mostly taken care within the OM. </li></ul>
  31. 31. What about CAML? <ul><li>Page Execution will no longer be driven by CAML (XML schema used in SharePoint) </li></ul><ul><li>CAML is still used in several places </li></ul><ul><ul><li>Field Type Definitions </li></ul></ul><ul><ul><li>Site and List Templates </li></ul></ul><ul><ul><li>View definitions </li></ul></ul>
  32. 32. SDK Available <ul><li>Documentation about V2 available at http://msdn.microsoft.com/sharepoint/ </li></ul>
  33. 33. Questions?
  34. 35. Code Example -- Enumerate Lists and Webs <ul><li>private void ShowSubWebs(HtmlTextWriter output) </li></ul><ul><li>{ </li></ul><ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul><ul><li>SPWebCollection mywebs = web.Webs; </li></ul><ul><li>foreach (SPWeb myweb in mywebs) </li></ul><ul><li>{ </li></ul><ul><li>output.Write(myweb.Title + &quot;<br>&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>private void ShowSubWebsWithLists(HtmlTextWriter output) </li></ul><ul><li>{ </li></ul><ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul><ul><li>SPWebCollection mywebs = web.Webs; </li></ul><ul><li>foreach (SPWeb myweb in mywebs) </li></ul><ul><li>{ </li></ul><ul><li>output.Write(&quot;<b>&quot; + myweb.Title + &quot;<br>&quot; + &quot;</b>&quot;); </li></ul><ul><li>SPListCollection lists = myweb.Lists; </li></ul><ul><li>foreach (SPList list in lists) </li></ul><ul><li>{ </li></ul><ul><li>if (list.ItemCount>10) </li></ul><ul><li>{ </li></ul><ul><li>output.Write(list.Title + &quot;: &quot; + list.ItemCount + &quot;<br>&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  35. 36. Code Snippet – Copy Files <ul><li>private SPWeb web; </li></ul><ul><li>private void Page_Load(object sender, System.EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>web = SPControl.GetContextWeb(Context); </li></ul><ul><li>} </li></ul><ul><li>private void Button1_Click(object sender, System.EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>int maxsize = Convert.ToInt32(TextBox1.Text); </li></ul><ul><li>SPFolder myfolder=web.GetFolder(&quot;Shared Documents&quot;); </li></ul><ul><li>SPFileCollection myfiles = myfolder.Files; </li></ul><ul><li>foreach (SPFile file in myfiles) </li></ul><ul><li>{ </li></ul><ul><li>if (file.Length>(maxsize*1024)) </li></ul><ul><li>{ </li></ul><ul><li>Response.Write(file.Name + &quot;: &quot; + file.Length/1024 + &quot;kb<br>&quot;); </li></ul><ul><li>file.CopyTo(&quot;Archive/&quot;+file.Name,true); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  36. 37. Code Snippet – Add users <ul><li>private void Button1_Click(object sender, System.EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>SPWeb web = SPControl.GetContextWeb(Context); </li></ul><ul><li>string username = TextBox1.Text; </li></ul><ul><li>string displayname = TextBox2.Text; </li></ul><ul><li>string email = TextBox3.Text; </li></ul><ul><li>SPRole admins = web.Roles[&quot;Administrator&quot;]; </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>admins.AddUser(username,email,displayname,&quot;&quot;); </li></ul><ul><li>Label4.Text=&quot;Successfully added user&quot;; </li></ul><ul><li>} </li></ul><ul><li>catch(Exception ex) </li></ul><ul><li>{ </li></ul><ul><li>Label4.Text=ex.ToString(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×