Code Snippet – Creating context objects in SharePoint 2013 / 2010

Code Snippet – Creating context objects in SharePoint 2013 / 2010
4 votes, 5.00 avg. rating (98% score)

Introduction

Code snippet how to create site context objects using Server Object Model, Managed Client Object Model, Silver Light Object Model, Javascript Object Model in SharePoint 2013/2010

Creating Context

Server Object Model

SPSite siteCollection = SPContext.Current.Site;
string url = siteCollection.Url;

Managed Client Object Model

using (ClientContext ctx = new ClientContext("http://myintranet.com"))
{
    Site siteCollection = ctx.Site;
    ctx.Load(siteCollection);
    ctx.ExecuteQuery();
    string url = siteCollection.Url;
}



Silverlight Client Object Model

using (ClientContext ctx =
    new ClientContext("http://myintranet.com"))
{
    Site siteCollection = ctx.Site;
    ctx.Load(siteCollection);
    ctx.ExecuteQuery();
    string url = siteCollection.Url;
}

Javascript Client Object Model

var siteCollection;
function getSiteCollection
{
    var ctx = new SP.ClientContext("/");
    siteCollection = ctx.get_site;
    ctx.load(site);
    ctx.executeQueryAsync(success, failure);
}

function success {
    string url = siteCollection.get_url;
}

function failure {
    alert("Failure!");
}



The ClientContext class in the Managed, Silverlight, and Mobile object models inherits from the ClientContextRuntime class.
The SP.ClientContext class in the JavaScript client object model inherits from the SP.ClientContextRuntime class and provides equivalent functionality to the ClientContext class found in the Managed, Silverlight, and Mobile client object models.

January 19, 2014 В· Adi В· No Comments
Tags: , , ,  В· Posted in: C#, Sharepoint 2010, SharePoint 2013

Leave a Reply

What is 13 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)