1

I am trying to upload image to SharePoint Picture Library. This is my code

 String sharePointSite = "http://mysite/";
 String myLibraryName = "ImageLibrary"
 using (var site = new SPSite(sharePointSite))
 {
     using (SPWeb web = site.OpenWeb())
     {
          SPPictureLibrary addImage = (SPPictureLibrary)web.Lists[myLibraryName ];

          addImage.RootFolder.Files.Add("test.jpg", imageResponse, true);
     }
 }

But I get error

at Microsoft.SharePoint.SPFile.CloneStreamToSPFileStream(SPWeb web, Stream stream, Int64 bytesToCopy) at Microsoft.SharePoint.SPFile.CloneStreamIfNeeded(SPWeb web, Stream inputStream, Int64 bytesToSave, SPFileStream& outStream) at Microsoft.SharePoint.SPFileCollection.AddStreamOrBytesInternal(String urlOfFile, Stream file, Int64 fileSizeToSave, SPFileStreamManager spmgr, PutFileOpt fileOpt, String createdBy, String modifiedBy, Int32 createdByID, Int32 modifiedByID, DateTime timeCreated, DateTime timeLastModified, Object varProperties, String checkInComment, Stream formatMetadata, String lockIdMatch, String etagToMatch, SPLockType lockType, String lockId, TimeSpan lockTimeout, Boolean validateRequiredFields, SPVirusCheckStatus& virusCheckStatus, String& virusCheckMessage, String& etagNew, Boolean& ignoredRequiredProps) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Stream file, Boole an overwrite, String checkInComment, Boolean checkRequiredFields) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Stream file, Boolean overwrite) at ConsoleApplication2.Program.Main(String[] args) in c:\Users\i401057\Documents\Visual Studio 2013\Projects\SharePointProject4\ConsoleApplication2\Program.cs:line 88 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

4
  • 3
    How are you populating imageResponse? Apr 27, 2015 at 13:39
  • @AmalHashim I following this link to get image. gist.github.com/fraga/4108898 Apr 27, 2015 at 13:45
  • 1
    You need to read response into a byte array before adding.. can you post complete code. Apr 27, 2015 at 13:50
  • @AmalHashim sorry i solved answer myself before i saw your comment. Sorry really. But many thanks Apr 28, 2015 at 11:52

1 Answer 1

0

I solved the issue. As pointed by @Amal Hashim i am reading response in byte array. complete code is

var webClient = new WebClient();
byte[] converttobyte= webClient.DownloadData(imageUrl);


var imageId = image.SelectToken("id");

String sharePointSite = "http://mysite/";
String documentLibraryName = "ImageLibrary";

using (var site = new SPSite(sharePointSite))
{
      using (SPWeb web = site.OpenWeb())
      {
           SPPictureLibrary pics = (SPPictureLibrary)web.Lists[documentLibraryName];

            pics.RootFolder.Files.Add((string) imageId + ".jpg", converttobyte, true);


       }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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