Showing posts with label Rest api. Show all posts
Showing posts with label Rest api. Show all posts

Sunday 16 October 2016

SharePoint REST API $expand query option.

A few months ago, I got a task that only logged-in users can edit list items created by themselves, which requires the Created By (Author) and Modified By (Editor) field values of that list item.

So, I will share my experience in resolving it. The Created By and Modified By values doesn't appear when a GET request is done; only AuthorId and EditorId are returned, as given below.


The Created By (Author) and Modified By (Editor) fields are of the People type. If you want the Author and Editor Title values, then you have to use the REST API $expand query option. The $expand query option is used to lookup column values, or you can think of it as a JOIN on two lists like in SQL for getting data. The SharePoint REST API supports OData query options. In this scenario, the lookup column points to the User Information List to get the person's details.

The code snippet below defines the proper usage of the $expand query option in the REST API. The code expands the Author and Editor entity fields, and in the $select column name, a forward slash (/) is used.


The success response object is logged using the console API.



The below image shows all the values returned.



🚀 "Happy Coding" 🚀

Saturday 10 October 2015

SharePoint activates the feature using the REST API.

The REST API provides a simple and easy way to activate features at the site and web level..

I have created simple JavaScript code to activate the feature.

function activateFeature() {
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/features/add('7e2e2482-9007-4db3-8a7f-9bccd986ec4e')",
        type: "POST",
        headers: {
            "Accept": "application/json;odata=verbose", //return data format.
           "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
// perform operation on the response object.
        },
        error: function (err) {
            alert("Error: " + JSON.stringify(err));
        }
    });
}


Change the URI, and the feature will be activated based on the endpoint.

  1. /web - then feature will be activated at the web level.
  2. /site - then feature will be activated at the site collection level.

Now pass the feature GUID into the add method. You can get the GUID value by using the PowerShell command or by "SharePoint Manager 2013".


🚀 "Happy Coding" 🚀

Saturday 8 August 2015

SharePoint 2013 REST API Get requests from the designer workflow.

SharePoint 2013 provides a completely new workflow engine, which has a new "Call HTTP web service" action that enables us to communicate with the web service from our workflow.

In this blog post, I will show you how you can use this new action method for a REST API GET request to a SharePoint list.

I have created a list "WebServiceList" and inserted dummy data into it as shown below.


Open SharePoint Designer 2013 and create a workflow attached to the list created above. Use the platform type as SharePoint 2013 workflow; otherwise Call HTTP web service method will not be available.
In our normal CSOM(Client Side Object Model), we call the REST API to get data back in JSON format. You need these headers as:

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose

In the workflow add the "Build the Dictionary" action of above headers as:

Click on the three dots to add items to the dictionary.






Now, add the HTTP call web service method with type GET as given below:


Test your REST call in the browser as given below:

Add the header dictionary variable created just above to the web service and set the request type to "GET" as:


Then click OK.
Add the GET action, and now we will get the result from the response content variable as

Now get the CountItems of the Output variable (allTitles) from the get action.
Then take one variable and set it to "0" and insert Loop (Loop n times). Inside the loop again, add the GET action to get all titles from the variable JsonResult and log it. Add the Calculate action to add "1" to the loop index variable and then again set its value. Now end the workflow.
Complete workflow is given below


See the Tasks and Workflow History list details below; you will get the data fetched from the source list is logged in the history list.

Hope this will help you!


🚀"Happy Coding"🚀