4

Below is my method which is meant to copy a file from a folder on my desktop to the sharepoint document library shared documents.

Its giving me error on SaveChanges method call, error shows me this

"There is no file with URL 'http://---------/division1/shared documents/strikers_logo.jpg' in this Web " "An error occurred while processing this request."

 public void copytosharepoint()
        {
            try
            {
                ChsmainDataContext sharePoint = new ChsmainDataContext(new Uri("http://rajum-pc:40613/_vti_bin/listdata.svc"));
                sharePoint.Credentials = System.Net.CredentialCache.DefaultCredentials;

                using (FileStream file = File.Open(@"C:\Users\rmirani\Downloads" + workflowProperties.Item.File.Name, FileMode.Open))
                {

                    string path = "/division1/shared documents/"+workflowProperties.Item.File.Name;
                    string contentType = workflowProperties.Item.ContentType.Name;

                    SharedDocumentsItem documentItem = new SharedDocumentsItem()
                    {
                        ContentType = contentType,
                        Name = workflowProperties.Item.File.Name,
                        Path = path,
                        Title = workflowProperties.Item.File.Title
                    };

                    sharePoint.AddToSharedDocuments(documentItem);

                    sharePoint.SetSaveStream(documentItem, file, false, contentType, path);

                    sharePoint.SaveChanges();
                }

                // stream.Flush();
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("RHM Test Logs", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, null);

            }
        }
2
  • Unfortunately with no information of how ChsmainDataContext is internally trying to save the document to SharePoint library, it is pretty impossible to say what could be wrong :(
    – Jussi Palo
    Commented Jan 23, 2012 at 9:19
  • chsmaindatacontext is a sharepoint service Listdata.svc,
    – RHM
    Commented Jan 23, 2012 at 9:21

2 Answers 2

3

Apologies for not giving direct answer, but an alternative suggestion instead.

Would using copy.asmx be option?

"The copy web service enables you to create new documents and send the metadata for indexing in one call."

public static void CreateNewDocumentWithCopyService(string destUrl, string filename, byte[] fileContent) 
    { 
      CopySoapClient c = new CopySoapClient();
      c.UseDefaultCredentials = true;

      byte[] myBinary = fileContent;
      string destination = destUrl + "/" + filename; 
      string[] destinationUrl = { destination }; 

      CopyService.FieldInformation info1 = new CopyService.FieldInformation(); 
      info1.DisplayName = "Title"; 
      info1.InternalName = "Title"; 
      info1.Type = CopyService.FieldType.Text; 
      info1.Value = "new title"; 

      CopyService.FieldInformation info2 = new CopyService.FieldInformation(); 
      info2.DisplayName = "Modified By"; 
      info2.InternalName = "Editor"; 
      info2.Type = CopyService.FieldType.User; 
      //info2.Value = "-1;#servername\\testmoss"; 
      info2.Value = "-1;servername\\alex";

      CopyService.FieldInformation[] info = { info1, info2 }; 
      CopyService.CopyResult resultTest = new CopyService.CopyResult(); 
      CopyService.CopyResult[] result = { resultTest }; 

      try 
      { 

        //c.CopyIntoItems(destination, destinationUrl, info, myBinary, out result); 
        //c.CopyIntoItemsAsync(destination, destinationUrl, info, myBinary);
      } 
      catch (Exception ex) 
      { 

      } 

    }

Alternate code:

string file = "C:\Users\rmirani\Downloads" + workflowProperties.Item.File.Name;
    CreateNewDocumentWithCopyService(file);

    public static void CreateNewDocumentWithCopyService(string fileName)
            {
                copyservice.Copy c = new copyservice.Copy();
                c.Url = "http://rajum-pc:40613/_vti_bin/copy.asmx";
                c.UseDefaultCredentials = true;

                byte[] myBinary = File.ReadAllBytes(fileName);
                string destination = "http://rajum-pc:40613/division1/shared documents/" + workflowProperties.Item.File.Name;
                string[] destinationUrl = { destination };

                copyservice.FieldInformation info1 = new copyservice.FieldInformation();
                info1.DisplayName = "Title";
                info1.InternalName = "Title";
                info1.Type = copyservice.FieldType.Text;
                info1.Value = "new title";

                copyservice.FieldInformation info2 = new copyservice.FieldInformation();
                info2.DisplayName = "Modified By";
                info2.InternalName = "Editor";
                info2.Type = copyservice.FieldType.User;
                info2.Value = "-1;#servername\\testmoss";

                copyservice.FieldInformation[] info = { info1, info2 };
                copyservice.CopyResult resultTest = new copyservice.CopyResult();
                copyservice.CopyResult[] result = { resultTest };

                try
                {
                    //When creating new content use the same URL in the SourceURI as in the Destination URL argument

                    c.CopyIntoItems(destination, destinationUrl, info, myBinary, out result);
                }
                catch (Exception ex)
                {

                }

            }

From: http://sharepointfieldnotes.blogspot.com/2009/09/uploading-content-into-sharepoint-let.html

15
  • let me try, but one question I need to copy a document to another sharepoint farm site. Will the copy.asmx code work in this scenario ?
    – RHM
    Commented Jan 23, 2012 at 10:49
  • I am referencing server/_vti_bin/copy.asmx in my code by this reference isnt working "copyservice.Copy" it cannot find the "Copy" class, whereas other classes it can see are "CopyResult" "CopySoapClient" etc
    – RHM
    Commented Jan 23, 2012 at 10:52
  • The same rules apply for authentication as with the listdata.svc where you used System.Net.CredentialCache.DefaultCredentials. Apart from that there should be no reason why it wouldn't work.
    – Jussi Palo
    Commented Jan 23, 2012 at 10:52
  • I got the Copy class now. Now trying your code.
    – RHM
    Commented Jan 23, 2012 at 10:56
  • I am running the above code, it runs but file is not copied to the second sharepoint server's shared document's folder.
    – RHM
    Commented Jan 24, 2012 at 9:56
2

The contenttype parameter in the SetSaveStream expects a valid Mime type, which is ideally the type of file you are uploading, if you are not sure then I would recommend using "*/*" instead.

Your modified code should look like

sharePoint.AddToSharedDocuments(documentItem);

sharePoint.SetSaveStream(documentItem, file, false, "*/*", path);

sharePoint.SaveChanges();
2
  • SetSaveStream stream method is from lisdata.svc right ?
    – RHM
    Commented Jan 26, 2012 at 7:59
  • How to get a file from workflowproperties.item.file and put it in the method AddToSharedDocuments() method to get it saved in the shared documents
    – RHM
    Commented Jan 26, 2012 at 8:26

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.