Wednesday, January 23, 2019

Caml & Get access to SP using tokenhelper in webapi

//camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>";// to get only files
                    //camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query></Query></View>"; // to get all folders with files
                    //ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllFoldersQuery());




[HttpGet]
        [Route("api/GetRootFolders")]
        public List<SPItem> GetRootFolders()
        {
            //first connect to sharepoint online
            List<SPItem> allItems = new List<SPItem>();
            //first connect to sharepoint online
            string spSiteURL = ConfigurationManager.AppSettings["SharePointURL"];
            Uri siteUrl = new Uri(spSiteURL);
            string realm = TokenHelper.GetRealmFromTargetUrl(siteUrl);
            string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUrl.Authority, realm).AccessToken;

            try
            {
                using (var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUrl.ToString(), accessToken))
                {

                    Web web = clientContext.Web;
                    // We want to retrieve the web's properties.
                    clientContext.Load(web);
                    clientContext.Load(web);
                    clientContext.ExecuteQuery();
                    clientContext.Load(web.CurrentUser);
                    clientContext.ExecuteQuery();
                    string username = web.CurrentUser.LoginName;
                    string webName = web.Title;
                    var list = web.Lists.GetByTitle(ConfigurationManager.AppSettings["LibName"]);
                    clientContext.Load(list);
                    clientContext.ExecuteQuery();
                    CamlQuery camlQuery = new CamlQuery();
                 
                    ListItemCollection listItems = list.GetItems(camlQuery);
                    clientContext.Load<ListItemCollection>(listItems);
                    clientContext.ExecuteQuery();
                    foreach (var item in listItems)
                    {
                        if (item.FileSystemObjectType != FileSystemObjectType.File)
                        {
                            SPItem itemCol = new SPItem();
                            itemCol.Name = item["FileLeafRef"].ToString();
                            itemCol.IsFolder = "Yes";
                            allItems.Add(itemCol);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var errmsg = ex.Message;
            }
            return allItems;
        }

No comments:

Post a Comment

Building Secure Azure AI Foundry Agents with Managed Identity

  The credential problem nobody wants to own Every team that connects an Azure AI Foundry agent to Microsoft Graph, SharePoint, Outlook, or...