This Blog will provide an insight of how we can use Sharepoint List and its properties in Provider Hosted App. Assuming you already are familiar with Sharepoint and Provider Hosted App.
As we all know Provider Hosted App runs outside the Sharepoint farm, it can also be on a totally different server. So, the point rises how one can have access to Sharepoint lists to perform actions. The answer is not as difficult as it might seem.
When you create a new project for Provider Hosted App, you get two inbuilt classes:
These are the files that help your App to communicate with the Sharepoint. To get access to lists, web and all other properties of Sharepoint you will need to create a client context token using the methods provided to you by these classes.
Code Example of creating your client context:
private void GetContextToken()
// The following code gets the client context and Title property by using TokenHelper.
// To access other properties, the app may need to request permissions on the host web.
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
Session["clientContext"] = clientContext;
//Get the site name.
clientContext.Load(clientContext.Web, web => web.Title);
siteName = clientContext.Web.Title;
//Get the current user.
clientContext.Load(clientContext.Web.CurrentUser, currentUser => currentUser.LoginName);
loginName = clientContext.Web.CurrentUser.LoginName;
string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);
if (contextTokenString != null)
contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;
else if (!IsPostBack)
Response.Write("Could not find a context token.");
This chunk of code will help you create the context token that will be used to get the properties of Sharepoint.
Now, to get a list named "Projects" in your Sharepoint farm within your app:
itemsProject is a Sharepoint ListItemCollection that contains top 100 items form Projects List.
Now, Suppose we need to get items based on some criteria. For example we need to get all Tasks that come within a project. For this, first we will have the Tasks List from Sharepoint and then perform a CAML query based on our criteria:
If you will observe the above code chunk you will notice everything was similar to the above code but the line that made the difference is how CAML query was written. In the above example we simple used CreateAllItemsQuery Method to get all items. But in this example we created our own XML query for filtering the items as per our need.
There are number of operators that you can use to create your query for example:
For Equals - 'eq'
For GreaterThanEqualTo - 'Geq'
For LessThanEqualTo - 'Leq'
"FieldRef Name" contains the Name of the column we need to apply filter on. "Value Type" defines its Type.