3

I have a Provider Hosted App, one of it's functionality is to upload the images into sharepoint picture library.

I am able to store the picture into some folder, using the below code.

public ActionResult Upload(HttpPostedFileBase file)
    {

        var hostweb = HttpContext.Request["SPHostUrl"];
        Helper.WriteLog(hostweb);
        Uri hosturi = new Uri(hostweb);

        using (var clientContext = new ClientContext(hosturi))
        {
            //List Photos = clientContext.Web.Lists.GetByTitle("Photos");

            var fileinfo = new FileCreationInformation();               
            fileinfo.Content = Helper.ReadFully(file.InputStream);
            //This will convert the Input Stream to byte[]

            var fileName = Path.GetFileName(file.FileName);                
            var path = Path.Combine("E:/Photos", fileName);
            file.SaveAs(path);
            Helper.WriteLog(path);
        }
        return View();
    }

Now I want to store the Image to the Photos Library. How should I do it.

2

This is what we are supposed to do.

public ActionResult Upload(HttpPostedFileBase file)
    {

        var hostweb = HttpContext.Request["SPHostUrl"];           
        Uri hosturi = new Uri(hostweb);

        using (var clientContext = new ClientContext(hosturi))
        {    
            using (var fs = file.InputStream)
            {
                var fi = new FileInfo(file.FileName);
                //change fi.name so that it is unique
                var list = clientContext.Web.Lists.GetByTitle("Photos");
                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);                    
            }
        }
        return View();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.