Thursday, March 2, 2017

REST call in C# SharePoint application

Hi Friends,

I have a scenario like user(s) may or may not have an access to SharePoint library. but some of folders are shared with those.

Requirement: Need to show which folders they have an access.

Shared to User:





Result Shared URLs List: 




Please add:  using System.Web.Script.Serialization;

public void RestCall(SPWeb web)
        {
            try
            {               
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(web.Url + "/_api/web/lists/getbytitle('Test1')/items?$select=EncodedAbsUrl,Title,Category,ID&$orderby=ID asc&$filter=FSObjType eq 1");
                request.Method = "GET";
                request.Accept = "application/json;odata=verbose";
                request.ContentType = "application/json;odata=verbose";

                request.Credentials = new NetworkCredential("Spuser_Name", "Spuser_Password");
 // Note: working is in-progress of getting default login user credentials instead of passing user name and password
                WebResponse response = request.GetResponse();
                Data data = null;

                using (response)
                {
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        try
                        {
                            string jSON = reader.ReadToEnd();
                            data = serializer.Deserialize<Data>(jSON);
                        }
                        catch (Exception ex)
                        {
                           // throw error
                        }
                    }
                }
                string urls = "";
                List<getFolderDetails> AllAccessFolderUrls = new List<getFolderDetails>();
                foreach (getFolderDetails fol in data.d.results)
                {
                    urls = urls + fol.EncodedAbsUrl + " \n ";
                    AllAccessFolderUrls.Add(fol);
                }
                Label1.Text = urls;
            }
            catch (Exception ex)
            {  }
        }

        public class Data
        {
            public Results d { get; set; }
        }
        public class Results
        {
            public getFolderDetails[] results { get; set; }
        }
        public class getFolderDetails
        {
            public string id { get; set; }
            //public string Title { get; set; }
            public string EncodedAbsUrl { get; set; }
        }



No comments:

Post a Comment