Upload a file to SharePoint through the built-in web services

后端 未结 7 847
悲&欢浪女
悲&欢浪女 2020-11-27 03:37

What is the best way to upload a file to a Document Library on a SharePoint server through the built-in web services that version WSS 3.0 exposes?

Following

相关标签:
7条回答
  • 2020-11-27 04:19
    public static void UploadFile(byte[] fileData) {
      var copy = new Copy {
        Url = "http://servername/sitename/_vti_bin/copy.asmx", 
        UseDefaultCredentials = true
      };
    
      string destinationUrl = "http://servername/sitename/doclibrary/filename";
      string[] destinationUrls = {destinationUrl};
    
      var info1 = new FieldInformation
                    {
                      DisplayName = "Title", 
                      InternalName = "Title", 
                      Type = FieldType.Text, 
                      Value = "New Title"
                    };
    
      FieldInformation[] info = {info1};
      var copyResult = new CopyResult();
      CopyResult[] copyResults = {copyResult};
    
      copy.CopyIntoItems(
        destinationUrl, destinationUrls, info, fileData, out copyResults);
    }
    

    NOTE: Changing the 1st parameter of CopyIntoItems to the file name, Path.GetFileName(destinationUrl), makes the unlink message disappear.

    0 讨论(0)
  • 2020-11-27 04:23

    I've had good luck using the DocLibHelper wrapper class described here: http://geek.hubkey.com/2007/10/upload-file-to-sharepoint-document.html

    0 讨论(0)
  • 2020-11-27 04:30

    Not sure on exactly which web service to use, but if you are in a position where you can use the SharePoint .NET API Dlls, then using the SPList and SPLibrary.Items.Add is really easy.

    0 讨论(0)
  • 2020-11-27 04:38

    Another option is to use plain ol' HTTP PUT:

    WebClient webclient = new WebClient();
    webclient.Credentials = new NetworkCredential(_userName, _password, _domain);
    webclient.UploadFile(remoteFileURL, "PUT", FilePath);
    webclient.Dispose();
    

    Where remoteFileURL points to your SharePoint document library...

    0 讨论(0)
  • 2020-11-27 04:40

    Example of using the WSS "Copy" Web service to upload a document to a library...

    public static void UploadFile2007(string destinationUrl, byte[] fileData)
    {
        // List of desination Urls, Just one in this example.
        string[] destinationUrls = { Uri.EscapeUriString(destinationUrl) };
    
        // Empty Field Information. This can be populated but not for this example.
        SharePoint2007CopyService.FieldInformation information = new 
            SharePoint2007CopyService.FieldInformation();
        SharePoint2007CopyService.FieldInformation[] info = { information };
    
        // To receive the result Xml.
        SharePoint2007CopyService.CopyResult[] result;
    
        // Create the Copy web service instance configured from the web.config file.
        SharePoint2007CopyService.CopySoapClient
        CopyService2007 = new CopySoapClient("CopySoap");
        CopyService2007.ClientCredentials.Windows.ClientCredential = 
            CredentialCache.DefaultNetworkCredentials;
        CopyService2007.ClientCredentials.Windows.AllowedImpersonationLevel = 
            System.Security.Principal.TokenImpersonationLevel.Delegation;
    
        CopyService2007.CopyIntoItems(destinationUrl, destinationUrls, info, fileData, out result);
    
        if (result[0].ErrorCode != SharePoint2007CopyService.CopyErrorCode.Success)
        {
            // ...
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:42

    From a colleage at work:

    Lazy way: your Windows WebDAV filesystem interface. It is bad as a programmatic solution because it relies on the WindowsClient service running on your OS, and also only works on websites running on port 80. Map a drive to the document library and get with the file copying.

    There is additionally a web service to upload files, painful but works all the time.

    I believe you are able to upload files via the FrontPage API but I don’t know of anyone who actually uses it.

    0 讨论(0)
提交回复
热议问题