Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);

            }
        }
share|improve this question
    
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 Jan 23 '12 at 9:19
    
chsmaindatacontext is a sharepoint service Listdata.svc, –  RHM Jan 23 '12 at 9:21
add comment

2 Answers 2

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

share|improve this answer
    
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 Jan 23 '12 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 Jan 23 '12 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 Jan 23 '12 at 10:52
    
I got the Copy class now. Now trying your code. –  RHM Jan 23 '12 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 Jan 24 '12 at 9:56
show 10 more comments

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();
share|improve this answer
    
SetSaveStream stream method is from lisdata.svc right ? –  RHM Jan 26 '12 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 Jan 26 '12 at 8:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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