CSOM – Get site object using JavaScript

Working with CSOM using JavaScript is slight different in syntax, but the process involved with getting the objects required for coding is the same! You still need to use the context object, load the context object, and execute the query that will send the context object off to SharePoint to tell it what you want and have it returned back.

The syntax is slightly different and the context return object is used in a different way:

var ctx;

var web;

var lists;


function onPageLoad()


ExecuteOrDelayUntilScriptLoaded(LoadAll, “sp.js”);


function LoadAll()


ctx = SP.ClientContext.get_current();

web = ctx.get_web();



lists = web.get_lists();


ctx.executeQueryAsync(doSomething, displayError);


function doSomething()


var siteTitle = web.get_title();

var siteId = web.get_id().toString();

var siteUrl = web.get_url();

var currentUser = web.get_currentUser().get_loginName();


function onError(sender, args)




CSOM – Get lists via Managed Code

I have been working on CSOM recently and I wanted to share this snipplet of code that I used to get the Web object, List Collection, and create a new list via the client object model through managed C# code.

// Create the client context by passing in the web url I want to use

ClientContext cctx = new ClientContext(“http://phillau.com”);

cctx.Credentials = CredentialCache.DefaultCredentials;

Web ctxWeb = cctx.Web;

ListCollection lists = ctxWeb.Lists;

// Load the context object with instructions to only populate the title field in the site object

cctx.Load(site s => s.Title);

// Request gets send to retrieve the site title


Console.WriteLine(“Site Title:” + ctxWeb.Title);

//Create a new list. Specify what you want it to be!

ListCreationInformation list = new ListCreationInformation();

list.Title = “Ships”;

list.Url = “List/Ships”;

list.TemplateType = (int)ListTemplateType.GenericList;

// Add to list collection


// Load into context object to be launched!


// Submit list blueprint!


foreach(List list in lists)