Download List Template from List Template Gallery Programmatically using CSOM

Download List Template from List Template Gallery Programmatically using CSOM

Download List Template from List Template Gallery Programmatically using CSOM Project Online.We save SharePoint List as.STP file or list template. All files saved in List Template Gallery. .STP file or list template can be download as manually. When we need to download files in bulk then we need to write some code. So below is code to Download List Template from List Template Gallery Programmatically using CSOM.

If you don’t know to add references in your solutions then follow the post below

Add  references in Solution

string listTitle = "List Template Gallery";
                string listItemId = "";
                string filePath = location;// Physical Location of your hard drive
                List list = clientContext.Web.Lists.GetByTitle(listTitle);
                CamlQuery query = CamlQuery.CreateAllItemsQuery();

                ListItemCollection listItemcollection = list.GetItems(query);
                clientContext.Load(listItemcollection);
                clientContext.ExecuteQuery();

[AdSense-A]

using (ClientContext clientContext = new ClientContext(pwaPath))
            {
                SecureString passWord2 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord2.AppendChar(c);
                clientContext.Credentials = new SharePointOnlineCredentials(UserName, passWord2);
                Web web = clientContext.Web;

                string listTitle = "List Template Gallery";
                string listItemId = "";
                string filePath = location;
                List list = clientContext.Web.Lists.GetByTitle(listTitle);
                CamlQuery query = CamlQuery.CreateAllItemsQuery();

                ListItemCollection listItemcollection = list.GetItems(query);
                clientContext.Load(listItemcollection);
                clientContext.ExecuteQuery();

                foreach (ListItem listItem in listItemcollection)
                {
                    clientContext.Load(list);
                    clientContext.Load(listItem, i => i.File);
                    clientContext.ExecuteQuery();
                    string FileNa = listItem.File.Name;
                        var fileRef = listItem.File.ServerRelativeUrl;
                        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
                        var fileName = Path.Combine(filePath, (string)listItem.File.Name);                       
                        using (var memory = new MemoryStream())
                        {
                            byte[] buffer = new byte[1024 * 64];
                            int nread = 0;
                            while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                memory.Write(buffer, 0, nread);
                            }
                            memory.Seek(0, SeekOrigin.Begin);
                            FileStream fddstream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                            fddstream.Write(buffer, 0, buffer.Length);
                            fddstream.Close();
                        }                    
                }
            }

Some useful Links are below

Get List Template from Project Site using CSOM Project Online

Get Project Site URL using CSOM Programmatically

Add TimeSheet Line Classification using CSOM Project Online

Add actual Hours in TimeSheet using CSOM Project Online

Read TimeSheet Data using CSOM Project Online

Read project level lookup type custom field has multiple values using csom

Read Task Level Lookup Type Custom Field using CSOM

Update Lookup Type Custom Field using CSOM


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!