REST URL’s For SharePoint

This post is a collection of REST endpoints which are very helpful in performing various operations in SharePoint. Sometimes while working on projects we come across various scenarios where we have to use REST but we don’t know if such thing is possible or not using REST or whether any endpoint is available in REST for this.

I will try to cover some scenarios where i used REST and they are pretty useful and easy to use. I will keep on updating this post as and when possible.

If you know some endpoints which are really very useful and are not mentioned in this post, you can suggest them in comments. I will try to incorporate it in my post.

Endpoints that are helpful in read operation map to HTTP GET. Update operation map to HTTP POST, for Update and Insert operations map to HTTP PUT and to Delete SharePoint object it maps to HTTP DELETE.

List Operations:

  • Url to get all items:

/_api/Web/Lists/GetByTitle(‘List Title’)/Items

  • Select query on List to get Selected Items:

/_api/Web/Lists/GetByTitle(”List Title)/Items?$select=column1,column2

  • To get user details like ‘FirstName’, ‘LastName’, ‘EMail’, etc for a Person/Group field of SharePoint List we need to make use of ‘expand’ in REST API. In REST API we get id instead of actual value because person/group field is a lookup field internally.

/_api/Web/Lists/GetByTitle(‘List Title’)/Items?$select=column1,column2,column3/EMail&$expand=column3/Id

listitemexpand

  • Get Attachment from List Item

_api/Web/Lists/GetByTitle(‘UserData’)/items?$select=AttachmentFiles&$expand=AttachmentFiles

listitemattachment

 Working With Libraries

  •  You can retrieve the root folder of your Document Library using the following endpoint:          

/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)

  • To retrieve all files in a folder:

/_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)/Files

  • To retrieve a specific file:

   /_api/web/GetFolderByServerRelativeUrl(‘/Shared Documents’)/Files(‘Test.docx’)

documentlibfilejpg

Get User Properties with REST api

  • Get properties of current user

_api/SP.UserProfiles.PeopleManager/GetMyProperties

  • Get specific properties for current user

_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email,DisplayName,PictureUrl,AccountName

Making REST calls using C#

Here is how to make REST calls using C# code. It’s easier to make REST call using JavaScript but it is little bit tricky to make REST calls using C#.

I will show simple example to update list item using REST in a console application.

While updating or Creating list items we need Form Digest. So in below code, first of all we will have to get Form Digest using method GetFormDigest().


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.SharePoint.Client;
using System.Security;
using System.IO;
using System.Web.Script.Serialization;
namespace RestCallConsoleApp
{
class Program
{
static void Main(string[] args)
{
try
{
SharePointOnlineCredentials cred = EnterCredentails();
UpdateListItem(cred);
}
catch (Exception ex)
{
}
}
public static string GetFormDigest(SharePointOnlineCredentials cred)
{
string formDigest = null;
string resourceUrl = "https://personal344.sharepoint.com/_api/contextinfo";
HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
wreq.Credentials = cred;
wreq.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
wreq.Method = "POST";
wreq.Accept = "application/json;odata=verbose";
wreq.ContentLength = 0;
wreq.ContentType = "application/json";
wreq.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
string result;
WebResponse wresp = wreq.GetResponse();
using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
{
result = sr.ReadToEnd();
}
var jss = new JavaScriptSerializer();
var val = jss.Deserialize<Dictionary<string, object>>(result);
var d = val["d"] as Dictionary<string, object>;
var wi = d["GetContextWebInformation"] as Dictionary<string, object>;
formDigest = wi["FormDigestValue"].ToString();
return formDigest;
}
public static void UpdateListItem(SharePointOnlineCredentials cred)
{
string result = string.Empty;
Uri uri = new Uri("https://personal344.sharepoint.com/_api/web/lists/getbytitle('Test&#39;)/items(1)");
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(uri);
wreq.Credentials = cred;
string formDigest = GetFormDigest(cred);
wreq.Method = "POST";
wreq.Accept = "application/json; odata=verbose";
wreq.ContentType = "application/json; odata=verbose";
wreq.Headers.Add("X-HTTP-Method", "MERGE");
wreq.Headers.Add("IF-MATCH", "*");
wreq.Headers.Add("X-RequestDigest", formDigest);
wreq.Headers.Add("Authorization", "BEARER" + formDigest);
string stringData = "{'__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'I am updated Title now'}";
wreq.ContentLength = stringData.Length;
StreamWriter writer = new StreamWriter(wreq.GetRequestStream());
writer.Write(stringData);
writer.Flush();
WebResponse wresp = wreq.GetResponse();
using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
{
result = sr.ReadToEnd();
}
}
public static SharePointOnlineCredentials EnterCredentails()
{
ConsoleColor defaultForeground = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Enter your loginid:");
Console.ForegroundColor = defaultForeground;
string userName = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Enter your password.");
Console.ForegroundColor = defaultForeground;
SecureString password = GetPassword();
SharePointOnlineCredentials cred = new SharePointOnlineCredentials(userName, password);
return cred;
}
private static SecureString GetPassword()
{
ConsoleKeyInfo info;
//Get the user's password as a SecureString
SecureString securePassword = new SecureString();
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while (info.Key != ConsoleKey.Enter);
return securePassword;
}
}
}

view raw

program.cs

hosted with ❤ by GitHub

This is complete program.cs. This is tried and tested code and it works well.

Let me know your thoughts on comments below.

Enjoy reading.