This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var web = clientContext.Web; | |
clientContext.Load(web, w => w.Title); | |
clientContext.ExecuteQuery(); |
Now in this case, the second parameter of the clientContext.Load method is an object of type Expression<Func<Web, object>>[]
This is an array of Linq expressions which can be utilized to our benefit. We can convert that array into a parameter which can be passed to a "Utility" function. This function will only get the properties specified in that array. Like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Web GetWebDetails(ClientContext clientContext, Expression<Func<Web, object>>[] retrievals) | |
{ | |
var web = clientContext.Web; | |
clientContext.Load(web, retrievals); | |
clientContext.ExecuteQuery(); | |
return web; | |
} |
Only get the Title and Id of the Web:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Only get essential web info | |
var infoRetrievals = new Expression<Func<Web, object>>[] | |
{ | |
w => w.Title, | |
w => w.Id, | |
}; | |
var webInfo = GetWebDetails(clientContext, infoRetrievals); | |
Console.WriteLine(webInfo.Title); | |
Console.WriteLine(webInfo.Id); | |
//Console.WriteLine(webInfo.Description); //This will throw an error because we are not fetching that property. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Only get Styling Related Details | |
var styleRetrievals = new Expression<Func<Web, object>>[] | |
{ | |
w => w.MasterUrl, | |
w => w.CustomMasterUrl | |
}; | |
var webStyle = GetWebDetails(clientContext, styleRetrievals); | |
Console.WriteLine(webStyle.MasterUrl); | |
Console.WriteLine(webStyle.CustomMasterUrl); | |
//Console.WriteLine(webStyle.AlternateCssUrl); //This will throw an error because we are not fetching that property. |
You can also have other utility functions for Lists, Users etc. Here is a similar function for Lists:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List GetListDetails(ClientContext clientContext, string listTitle, Expression<Func<List, object>>[] retrievals) | |
{ | |
var list = clientContext.Web.Lists.GetByTitle(listTitle); | |
clientContext.Load(list, retrievals); | |
clientContext.ExecuteQuery(); | |
return list; | |
} |