2010-03-18 76 views
1

以下的C#片始终与web服务CopyIntoItems不工作上传文件到SharePoint

未知

对象引用不设置为一个对象的一个​​实例失败

有人知道我错过了什么吗?

 try 
     { 
      //Copy WebService Settings     
      String strUserName = "abc"; 
      String strPassword = "abc"; 
      String strDomain = "SVR03"; 
      String FileName = "Filename.xls"; 

      WebReference.Copy copyService = new WebReference.Copy(); 
      copyService.Url = "http://192.168.11.253/_vti_bin/copy.asmx"; 
      copyService.Credentials = new NetworkCredential 
              (strUserName, 
              strPassword, 
              strDomain); 


      // Filestream of attachment 
      FileStream MyFile = new FileStream(@"C:\temp\28200.xls", FileMode.Open, FileAccess.Read); 
      // Read the attachment in to a variable 
      byte[] Contents = new byte[MyFile.Length]; 
      MyFile.Read(Contents, 0, (int)MyFile.Length); 
      MyFile.Close(); 

      //Change file name if not exist then create new one     
      String[] destinationUrl = { "http://192.168.11.253/Shared Documents/28200.xls" }; 

      // Setup some SharePoint metadata fields 
      WebReference.FieldInformation fieldInfo = new WebReference.FieldInformation(); 
      WebReference.FieldInformation[] ListFields = { fieldInfo }; 

      //Copy the document from Local to SharePoint 
      WebReference.CopyResult[] result; 
      uint NewListId = copyService.CopyIntoItems 
       (FileName, 
       destinationUrl, 
       ListFields, Contents, out result); 
      if (result.Length < 1) 
       Console.WriteLine("Unable to create a document library item"); 
      else 
      { 
       Console.WriteLine(result.Length);  
       Console.WriteLine(result[0].ErrorCode); 
       Console.WriteLine(result[0].ErrorMessage); 
       Console.WriteLine(result[0].DestinationUrl); 
      } 
     } 

     catch (Exception ex) 
     { 
      Console.WriteLine("Exception: {0}", ex.Message); 
     } 
+0

你应该调试一些。哪个对象变为空? – 2012-01-09 18:19:02

+0

仅当已经有一个名为“想要在服务器上上传的文档”的文档时,才会出现此行为? – Harry 2012-04-11 09:09:40

回答

0

不了解更多关于您的具体错误,我也会抓住吸管。它看起来像你的destinationUrl是一个不完整的路径。您通常需要指定整个网址到网站或网站集。所以,我希望你的destinationUrl是像http://192.168.11.253/[SiteName]/Shared Documents/28200.xls而不是http://192.168.11.253/Shared Documents/28200.xls"