2. SharePoint Developer APIs
• Server Object Model
Used by client apps running on SP server
• Client Object Models (CSOM)
Remote API
Three entry points: .NET Managed, Silverlight, JavaScript
Façade layer on top of WCF service
Uses batching model to access resources
• REST Web Services (API)
SP 2010: CRUD on list data only
SP 2013: API expanded to be more like CSOM
• SharePoint Web Services
“Legacy” SOAP-based web services
3. Server Object Model
• Can be used when “in the context” of SharePoint
Code-behind, event handlers, timer jobs
ASP.NET applications running in same app. pool
Client applications that run on SharePoint servers
• API implemented in Microsoft.SharePoint.dll
• Core types map to main SharePoint components
SPSite, SPWeb, SPList, SPDocumentLibrary,
SPListItem
SPContext gives access to current context
4. Server Object Model
• The SharePoint version of “Hello, World”
Show the root site of a collection and it’s lists
using (var site = new SPSite("http://localhost/sites/demo/"))
{
var web = site.RootWeb;
ListBox1.Items.Add(web.Title);
foreach (SPList list in web.Lists)
{
ListBox1.Items.Add("t" + list.Title);
}
}
5. Resource Usage
• SPSite and SPWeb objects use unmanaged resources
Vital that you release resources with Dispose
• General rules:
If you create the object, you should Dispose
var site = new SPSite(“http://localhost”);
var web = site.OpenWeb();
If you get a reference from a property, don’t Dispose
var web = site.RootWeb
There are exceptions to these rules
• Use SPDisposeCheck to analyze code
7. Event Handlers
• Override methods on known receiver types
SPFeatureReceiver
SPListEventReceiver
SPItemEventReceiver
• Register receiver as handler for entity
Use CAML or code
• Synchronous and asynchronous events
ItemAdding
ItemAdded
8. Sample Feature Receiver
public class Feature1EventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
web.Properties["OldTitle"] = web.Title;
web.Properties.Update();
web.Title = "Feature activated at " + DateTime.Now.ToLongTimeString();
web.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
web.Title = web.Properties["OldTitle"];
web.Update();
}
}
10. Client Object Model
• API used when building remote applications
Three entry points: .NET Managed, Silverlight, ECMAScript
Alternative to SharePoint ASMX Web services
• Designed to be similar to the Server Object Model
• Types in COM generally named the same as SOM minus
‘SP’ prefix
• Methods and properties also named the same when
possible
• Many SOM types or members are not available in COM
Example: the COM does not have WebApplication or Farm types
11. Retrieving Resources using Load
• Retrieve object data in next batch
• Object properties loaded in-place
• Some properties not retrieved automatically
Example: child collection properties
• Can explicitly indicate properties to retrieve
var siteUrl = "http://localhost/sites/demo";
var context = new ClientContext(siteUrl);
var web = context.Web;
context.Load(web, w => w.Title, w => w.Description);
context.ExecuteQuery();
Console.WriteLine(web.Title);
12. Retrieving Resources using LoadQuery
• Result of query included in next batch
• Returns enumerable result
var query = from list in web.Lists.Include(l => l.Title)
where list.Hidden == false &&
list.ItemCount > 0
select list;
var lists = context.LoadQuery(query);
15. Silverlight Client Object Model
• Very similar to the .NET managed implementation
• Silverlight controls can be hosted inside or outside
SharePoint
Affects how ClientContext is accessed
In SharePoint Web Parts, site pages, and application pages
Use ClientContext.Current
In pages external to the SharePoint Web application
Create new ClientContext
• Service calls must be made asynchronously
ExecuteQueryAsync(succeededCallback, failedCallback)
18. JavaScript Client Object Model
• Similar to using .NET Managed/Silverlight implementations
Different platform and different language so slightly different API
• Can only be used on pages running in the context of
SharePoint
• Referenced using a SharePoint:ScriptLink control
Use ScriptMode=“Debug” to use debug version of library
• To get intellisense, also add <script> tag
Wrap in #if compiler directive so script isn’t loaded twice
• API uses conventions common in JavaScript libraries
Camel-cased member names
Properties implemented via get and set methods