Home > Software engineering >  Uploading Image using Microsoft.SharePoint.Client to a sharepoint folder
Uploading Image using Microsoft.SharePoint.Client to a sharepoint folder

Time:10-25

I am stuck with half code of C# to upload an image to a sharepoint folder. I can get the title but is there a way I can upload images.

actual url to the folder: 

https://mywebsite/sites/COLBMAOpsServ/ip/Forms/AllItems.aspx?id=/sites/COLBMAOpsServ/ip/01 Business Cases/PMO Tools & Information/IPA dump folder_SWP images&p=true&originalPath=aHR0cHM6Ly9zcG8uYmhwYmlsbGl0b24uY29tLzpmOi9zL0NPTEJNQU9wc1NlcnYvRW9nVC1hQl83NnBCaEFLYzdnZHlldGtCWnRmaGRZVkUzVHdrdTBFQ1cxLVFLUT9ydGltZT1OWmRmN2J1SDJVZw



public class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "https://mywebsite/sites/COLBMAOpsServ";
            
            using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "4a30b9e9-2222-3333-45678-e59af9bf8150", "QsyzIc09Rt3bBmWk4444444444M5KpLv9LX45678990U="))
            {
                cc.Load(cc.Web, p => p.Title);
                cc.ExecuteQuery();
                Console.WriteLine(cc.Web.Title);
            };
        }
    }

CodePudding user response:

You could upload a file to sharepoint folder like this:

        var uploadFilePath = @"C:\temp\test.jpg";
        var uploadFolderUrl = "/sites/yoursite/Shared Documents/folder";
        var fileCreationInfo = new FileCreationInformation
        {
            Content = System.IO.File.ReadAllBytes(uploadFilePath),
            Overwrite = true,
            Url = Path.GetFileName(uploadFilePath)
        };
        var targetFolder = ctx.Web.GetFolderByServerRelativeUrl(uploadFolderUrl);
        var uploadFile = targetFolder.Files.Add(fileCreationInfo);
        ctx.Load(uploadFile);
        ctx.ExecuteQuery();
  • Related