Upload List Template (.Stp) in List template gallery using CSOM Project Online

Upload List Template (.Stp) in List template gallery Programmatically using CSOM Project Online

Upload List Template (.Stp) in List template gallery Programmatically using CSOM Project Online. In this Post, you learn Upload List Template (.Stp) List template gallery using CSOM Project Online. When we save a list as the template then an STP file is created. A list template can be upload as manually.But when you required the uploading List template (.STP) file in bulk. Then you need to write code. So below is code for Upload List Template (.Stp) in List template gallery Programmatically using CSOM Project Online.

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 filePath = location; // Physical location where all .stp file are stored
List list = clientContext.Web.Lists.GetByTitle(listTitle);// All List from list template gallery

[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 strSTPFileName = string.Empty;
                string strSTPFileNameWithoutExt = string.Empty;
                string listTitle = "List Template Gallery";
                string filePath = location;

                List list = clientContext.Web.Lists.GetByTitle(listTitle);

                string[] fileCollection = Directory.GetFiles(filePath);
                foreach (string strfile in fileCollection)
                {
                    int lstIndexofSlash = strfile.LastIndexOf("\\");
                    strSTPFileName = strfile.Substring(lstIndexofSlash + 1);

                    strSTPFileNameWithoutExt = strSTPFileName.Substring(0, strSTPFileName.IndexOf("."));
                    if (strSTPFileNameWithoutExt == "demo1_temp")
                    {
                        FileStream fs = new FileStream(strfile, FileMode.Open);
                        var fi = new FileInfo(strfile);
                        clientContext.Load(list.RootFolder);
                        clientContext.ExecuteQuery();
                        var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true);
                    }
                }
            }

 


Comments

  1. Why share code if it cannot be copy/pasted? That’s extremely frustrating and goes against the point of having a blog.

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 !!