basic operation’s that SharePointer needs for SharePointing by using CSOM – part 1

Yes, SharePoint is awesome and I really enjoyed with developing apps on it, and in this article I’ll list down the basic operation that SharePoint developer needs while SharePointing  by using CSOM

We have four main things to deal with in SharePoint

  • Operations on SharePoint site  – Part 1
  • Operations on SharePoint List- Part 2
  • Operations on SharePoint List item -Part 3
  • Operations on SharePoint Users- Part 4

And I’ll start with SharePoint site operation

Ø  Get all Website properties

Retrieve the title of a SharePoint website.

C#

// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint. 
ClientContext context = new ClientContext("http://Developersportal.net"); 
// The SharePoint web at the URL.
Web web = context.Web;  
// We want to retrieve the web's properties.
context.Load(web);
// Execute the query to the server.
context.ExecuteQuery(); 
// Now, the web's properties are available and we could display
// web properties, such as title. 
label1.Text = web.Title;

 

However you can retrieve only selected properties of a website

If you are  interested only in a few properties of the Site object. The SharePoint  Framework CSOM (Client side object model)  does not require you to get all properties from the object on a serve, so  you can use lambda expressions, to specifically requested property names.

By using this technique you’ll reduce the unnecessary data transfer between the client and the server..

Note that you need to add a using statement for System.Linq.

C#

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net");  
// The SharePoint web at the URL.
Web web = context.Web;  
// We want to retrieve the web's title and description. 
context.Load(web, w => w.Title, w => w.Description);  
// Execute the query to server.
context.ExecuteQuery();  
// Now, only the web's title and description are available. If you 
// try to print out other properties, the code will throw 
// an exception because other properties are not available. 
label1.Text = web.Title;
label1.Text = web. Description;

 

Ø  Update SharePoint site’s properties

This example shows how to write to the website’s properties.

C#

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net");  
// The SharePoint web at the URL.
Web web = context.Web;  
web.Title = "New Title (Devalopers Portal Awesome Team)";
web.Description = "New Description";  
// Note that the web.Update() does not trigger a request to the server
// because the client library until ExecuteQuery() is called. 
web.Update();  
// Execute the query to server.
context.ExecuteQuery();

 

Ø  Create a new SharePoint site

This example shows how to create a new SharePoint site as a subsite of the current website. Use the WebCreationInformation class to create a new website. You also need to add using statements for System.Collections.Generic and System.Text.

C#

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net ");  
WebCreationInformation creation = new WebCreationInformation();
creation.Url = "sub site 1";
creation.Title = "Hello sub site 1"; 
Web newWeb = context.Web.Webs.Add(creation);
// Retrieve the new web information. c
ontext.Load(newWeb, w => w.Title); 
context.ExecuteQuery();  
label1.Text = newWeb.Title;

 

 

See You in Part 2

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *