Monday, 18 May 2015

Load all Publishing Pages Library from a site collection by Base Template ID


Suppose you want to load all the Pages library form a Sharepoint Site. For this we can check the Base Template ID's of each list. For our case, the Base Template ID of Pages document library is 850.
The following method illustrates the successfull loading of the Pages library to a dropdown control.




private void LoadDocLibraries(string token)
        {
         
                using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(SitePicker.SelectedNode.Value, token))
            {
                Web web = context.Web;
                ListCollection lists = context.Web.Lists;
                context.Load(web);

                context.Load(lists, all => all
               .Where(l => l.BaseTemplate == 850)  //TemplateId of Publishing Pages Library=850 
               .Include(l => l.Title, l => l.RootFolder.Name));
               context.ExecuteQuery();

               ddlLibrary.Items.Clear();

                    foreach (List list in lists)
                    {

                        var item = new DropDownListItem
                        {
                            Value = list.RootFolder.Name,
                            Text = list.Title
                        };
                        ddlLibrary.Items.Add(item);

                    }
                }
            }
            catch (Exception ex)
            {
                ddlLibrary.Items.Clear();
                throw ex;
            }
        }

No comments:

Post a Comment