I had a requirement
to get the pages from the SharePoint Pages Library which is tagged with Managed
metadata Property. I tried the rest call
with the $filter but did not get any result. Then I come to know that Managed
Metadata property cannot be filtered directly in the rest call. You need to
send the Caml Query for the get the filtered data.
Again There was a
challenge to create a dynamic CAML Query and pass it to the Rest call to get
the filtered page from the SharePoint Pages Library.
For this I made a
AJAX call to get the values for the Managed Metadata Property of the current
page. And Create a Caml Query based on the Values.
function GetCurrentPageProperties() {
$.ajax({
url: <site Url> + "/_api/web/lists/getbytitle('Pages')/items?$select=<Managed
Metadata Property>,FileLeafRef&$expand=File&$filter=FileLeafRef eq
'" +
document.location.pathname.substring(document.location.pathname.lastIndexOf('/') + 1, document.location.pathname.length) + "'",
type: "GET",
headers: { "accept": "application/json;odata=verbose" },
async: false,
success: function (data) {
query = "<Where>"
var totalCount = data.d.results.length;
for (var
resultCount = 0; resultCount < data.d.results.length; resultCount++) {
if
(data.d.results[resultCount].<Managed Metadata Property>.results.length
> 0) {
for (var count = 0; count <
data.d.results[resultCount]. ].<Managed Metadata
Property>.results.length; count++) {
if (count == totalCount - 1) {
query +=
createQuery("Managed
Metadata Property",
data.d.results[resultCount].<Managed Metadata
Property>.results[count].Label,1);
}
else {
query +=
createQuery("Managed
Metadata Property ",
data.d.results[resultCount].<Managed Metadata
Property>.results[count].Label,0);
}
}
}
}
for (var i = 0;
i <= totalCount; i++)
{
query += "</Or>";
}
query += "</Where>";
collectAllRelatedNews(query);
},
error: function (error) {
errorRelatedNewsHandler('Error calling service method from GetCurrentPageProperties: ' + error.message, containerId);
}
});
}
function createQuery(fieldName, value,flag) {
var html = "";
if (flag == 0) { html += "<Or><Eq><FieldRef Name='" + fieldName + "' /><Value
Type='TaxonomyFieldTypeMulti'>" + value + "</Value></Eq>"; }
else { html += "<Eq><FieldRef
Name='" + fieldName + "' /><Value
Type='TaxonomyFieldTypeMulti'>" + value + "</Value></Eq>"; }
return html;
}
The above method will give you the
Caml Query based on the current page Managed Metadata properties value.
Now we need to pass this caml query
to the Rest API. For this again we will make a AJAX call.
function GetFilteredPages(query) {
var requestData = {
"query":
{
"__metadata":
{ "type": "SP.CamlQuery" }
, "ViewXml": "<View><Query>" + query + "</Query></View>"
}
};
$.ajax({
url: <Site Url> + "/_api/web/lists/getbytitle('Pages')/GetItems",
type: "POST",
data:
JSON.stringify(requestData),
headers:
{
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose"
},
async: false,
success: function (data) {
// Here you will get your Filtered Page
},
error: function (error) {
}
});
}
To get the filtered Page URL and Title again you have
to make a AJAX call on the above filtered Data.
function extractFilteredPagedFromResult(oData) {
var Pages = [];
if (oData.length > 0) {
for (var i = 0;
i < oData.length; i++) {
var pageItems = [];
var pageItem = [];
$.ajax({
url: <Site Url> + "/_api/web/lists/getbytitle('Pages')/items?$select=Title,FileRef&$filter=ID
eq " + oData [i].ID,
type: "GET",
headers: { "accept": "application/json;odata=verbose" },
async: false,
success: function (data) {
pageItem = [];
pageItem.Title = data.d.results[0].Title;
pageItem.Url = data.d.results[0].FileRef;
pageItems.push(pageItem);
},
error: function (error) { }
});
if (pageItems.length > 0) {
Pages.push(pageItems)
}
}
}
}
No comments:
Post a Comment