In order to query on pages, we need to know the contentTypeId of Article page. The process is illustrated below:
var contentTypeId =
"0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3 900242457EFB8B24247815D688C526CD44D";
Microsoft.SharePoint.Client.ListItemCollection _collListItem = null;
var token = GetAccessToken();
using (var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUrl, token))
{
List list = clientContext.Web.Lists.GetByTitle(library);
var camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View><Query><Where><BeginsWith><FieldRef Name='ContentTypeId' /> <Value Type='ContentTypeId'>" +contentTypeId + "</Value></BeginsWith> </Where><QueryOptions></QueryOptions></Query></View>";
_collListItem = list.GetItems(camlQuery);
clientContext.Load(_collListItem);
clientContext.ExecuteQuery();
}
Now you can iterate through _collListItem to get/use the pages according to your requirements.
In order to get the Access Token, the below method can be used:
private string GetAccessToken()
{
string token = string.Empty;
try
{
if (ViewState["Sp_AccessToken"] == null)
{
EnsureChildControls();
if (HfAccessToken != null && !string.IsNullOrEmpty(HfAccessToken.Value))
{
token = HfAccessToken.Value;
}
else
{
string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);
if (contextTokenString == null)
{
string redirectUrl = TokenHelper.GetAppContextTokenRequestUrl(HostWebUrl,
HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.ToString()));
Response.Redirect(redirectUrl, true);
}
else
{
var contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
var contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);
var sharepointUrl = new Uri(HostWebUrl);
token = TokenHelper.GetAccessToken(contextToken,sharepointUrl.Authority).AccessToken;
token = TokenHelper.GetAccessToken(contextToken,sharepointUrl.Authority).AccessToken;
}
if (HfAccessToken != null && !string.IsNullOrEmpty(token))
{
HfAccessToken.Value = token;
}
}
}
else
token = ViewState["Sp_AccessToken"].ToString();
}
catch (Exception ex)
{
throw ex;
}
ViewState["Sp_AccessToken"] = token;
return token;
}
No comments:
Post a Comment