How the ClientContext of SharePoint CSOM works
If you are writing clientsite applications for SharePoint you can use for example the Client Site Object Modell (CSOM). In the CSOM you will need to use the ClientContext to communicate with the SharePoint, but how does it work?
Using the ClientContext is like going to the supermarket.First of all you are looking out to which supermarket you will go. Then you figure out what you need and write it down to a shopping list. After that you are going to the supermarket and get all the stuff you need.
So first of all lets get the SharePoint (supermarket) we want to get our stuff from.
After that we can take a look into our fridge and look what we need. So for example we need some cheese, that would be a cheese list and your listname would be like 'Cheeselist'.
Now we should know what we need so lets hold it together. In this case I build a query to get all data - so we need all the cheese that the store can give us.
Now we can write it down to our shopping list. You can use the 'Load'-method more than one time so it would be a list. In that case it is a list with one item.
After the shopping list is finished it is time to go to the supermarket and get all the stuff we need.
Now we were shopping and we got all the stuff at home. So lets have a look at all our cheese and take it into the fridge.
Using the ClientContext is like going to the supermarket.First of all you are looking out to which supermarket you will go. Then you figure out what you need and write it down to a shopping list. After that you are going to the supermarket and get all the stuff you need.
So first of all lets get the SharePoint (supermarket) we want to get our stuff from.
ClientContext ctxSupermarket = new ClientContext("http://mySharePointURL/mySupermarket/");
After that we can take a look into our fridge and look what we need. So for example we need some cheese, that would be a cheese list and your listname would be like 'Cheeselist'.
List cheeseList = ctxSupermarket.Web.Lists.GetByTitle("Cheeselist");
Now we should know what we need so lets hold it together. In this case I build a query to get all data - so we need all the cheese that the store can give us.
CamlQuery allCheese = CamlQuery.CreateAllItemsQuery(); ListItemCollection cheeseWeNeed = cheeseList.GetItems(allCheese);
Now we can write it down to our shopping list. You can use the 'Load'-method more than one time so it would be a list. In that case it is a list with one item.
ctxSupermarket.Load(cheeseWeNeed);
After the shopping list is finished it is time to go to the supermarket and get all the stuff we need.
ctxSupermarket.ExecuteQuery();
Now we were shopping and we got all the stuff at home. So lets have a look at all our cheese and take it into the fridge.
List<string> fridge = new List<string>(); foreach (ListItem Cheese in cheeseWeNeed) { // IMPORTANT: do not use the .ToString() method.. in case of null-values it will crash string NameOfTheCheese = (string)Cheese["Title"]; fridge.Add(NameOfTheCheese); }
Comments
Post a Comment