I got sucked in my project where I have upload some files at location ~siteurl/_catalogs/theme by using client site scripting like JS/JQuery/REST/CSOM ? I searched a lot but no luck. Please help me. It's on-premise 2016 server. We are building this solution as third party user is managing our servers and we cannot place these file directly by server access. Also,uploading a files from browser is a option but want to do this by script as there are 1000+ site collection. And manual process is not possible. At least should I am able to upload the files using client script.
1 Answer
The easiest way is to use CSOM. If it will run on the server, then just add reference to Microsoft.SharePoint.Client
and Microsoft.SharePoint.Clinet.Runtime
. If it will run on some client computer, then install SharePoint Server 2013 Client Components SDK and add the same references.
using Microsoft.SharePoint.Client;
namespace StackExchange.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (ClientContext cc = new ClientContext("https://sharepoint.domain.com"))
{
// Load only ServerRelativeUrl
cc.Load(cc.Web, x => x.ServerRelativeUrl);
cc.ExecuteQuery();
// Get list by url
List list = cc.Web.GetList(cc.Web.ServerRelativeUrl.TrimEnd('/') + "/_catalogs/theme");
// Create file the library
File file = list.RootFolder.Files.Add(new FileCreationInformation()
{
// You can also add content through Stream using ContentStream
Content = new byte[] { 0, 1, 2, 3 },
Url = "file-name.txt"
});
cc.ExecuteQuery();
}
}
}
}