使用Copy.CopyIntoItems Web服务将文件上载到SharePoint 2010时获取400错误请求

时间:2012-02-14 17:10:14

标签: java web-services sharepoint

SharePoint新手。

我尝试使用Java的CopyIntoItems Web服务方法将文档上传到SharePoint,但继续获得400 Bad Request。我使用Java的wsimport从.wsdl文件生成类文件。这是我的Java代码,包含生成的类。

public static void createDocument(CopySoap port) {
    String url = SoapPortProvider.spSiteUrl + "/Shared Documents/Temp Folder/test.txt";
    String sourceUrl = "http://null";

    byte[] content = IoUtil.getBytes(new File("C:/CopyFile/READ-ME.txt"));

    FieldInformation descInfo = new FieldInformation ();
    descInfo.setDisplayName("Test Doc");
    descInfo.setType(FieldType.TEXT);
    descInfo.setValue("Test uploaded file");        


    DestinationUrlCollection urls = new DestinationUrlCollection();
    urls.getString().add(url);

    FieldInformationCollection infos = new FieldInformationCollection ();
    infos.getFieldInformation().add(descInfo);  

    CopyResultCollection results = new CopyResultCollection ();
    Holder<CopyResultCollection> resultHolder = new Holder<CopyResultCollection>(results);

    Holder<Long> longHolder = new Holder<Long>(new Long(-1));

    port.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);

}

我的SOAP请求看起来像

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">    
         <SourceUrl>http://null</SourceUrl>
         <DestinationUrls>
            <string>https://www.mysite.com/sites/TestSite/Shared Documents/Temp Folder/test.txt</string>
         </DestinationUrls>
         <Fields>
            <FieldInformation Value="Test uploaded file" DisplayName="Test Doc" Type="Text"/>
         </Fields>
         <Stream>KioqTWFrZSBzdXJlIHRoZSBjb250ZW50IGlzIHVuZGVyIEM6L0NvcHlGaWxlLy4gICoqKg0KDQpUbyBydW46DQoNCjEuICBFZGl0IHRoZSBkZXBsb3kucHJvcHMgZmlsZS4gIFNwZWNpZnkgdGhlIHNvdXJjZSAoZm9sZGVyIHRoYXQgY29udGFpbiBpdGVtcyB0byBkZXBsb3kpLCBkZXN0aW5hdGlvbiAoZm9sZGVyIHRvIGRlcGxveSB0byksIGFuZCBmaWxlcyAodGhlIGl0ZW1zIHRvIGRlcGxveSkuDQoyLiAgRG91YmxlIGNsaWNrIG9uIGRlcGxveUN1c3RvbWl6YXRpb24uYmF0DQoNCk5vdGUgdGhhdCBhIGxvZyBvZiB0aGUgcHJvZ3Jlc3Mgd2lsbCBiZSBjcmVhdGVkIGluIEM6L0NvcHlGaWxlL2NvcHlGaWxlLmxvZyANCg0KTm90ZSB0aGF0LCBmb3IgcHJlY2F1dGlvbiwgZXhpc3RpbmcgZmlsZSB3aWxsIG5vdCBiZSBvdmVyd3JpdHRlbiwgaW5zdGVhZCB3aWxsIGJlIHNhdmVkIGFzIHlvdXJfY29kZV9maWxlLm9sZA==</Stream>
      </CopyIntoItems>
    </S:Body>
</S:Envelope>

我得到的回应是

null: HTTP/1.1 400 Bad Request
Content-length: 0
X-powered-by: ASP.NET
Server: Microsoft-IIS/7.5
Date: Tue, 14 Feb 2012 16:29:51 GMT
Microsoftsharepointteamservices: 14.0.0.5138

并没有告诉我什么。可能会遗漏什么?

2 个答案:

答案 0 :(得分:3)

我已经完全使用了以下代码:

    try {
        //Copy WebService Settings 
        string webUrl           = "http://sharepointportal.ABC.com/";
        WSCopy.Copy copyService = new WSCopy.Copy();
        copyService.Url         = webUrl + "/_vti_bin/copy.asmx";
        copyService.Credentials = new NetworkCredential("username", "****", "Domain");

        //Declare and initiates the Copy WebService members for uploading 

        string sourceUrl        = "C:\\Work\\Ticket.Doc";   

        //Change file name if not exist then create new one     
        string[] destinationUrl    = { "http://sharepointportal.ABC.com/personal/username/Document Upload/Testing Document/newUpload.Doc" };

        WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();

        WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();

        WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();

        fFiledInfo.DisplayName = "Description";

        fFiledInfo.Type        = WSCopy.FieldType.Text;

        fFiledInfo.Value       = "Ticket";

        WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo }; 

        FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read); 

        byte[] fileContents = new Byte[strm.Length]; 

        byte[] r = new Byte[strm.Length];

        int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
        strm.Close();
        //Copy the document from Local to SharePoint 

        uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray); 

        MessageBox.Show("Suceess");  

     }
     catch (Exception ex) { 
        MessageBox.Show(ex.Message);

     }

答案 1 :(得分:2)

知道了!初始化时我的代码中只有一个错误。以下是希望使用SharePoint和Java的任何人的工作代码。我使用JAX-WS wsimport工具从.wsdl文件生成类文件。您可以将工具直接指向WSDL的URL,例如https://my.site.come/sites/mysite/_vti_bin/copy.asmx?wsdl

public static CopySoap getPort(String username, String password)  {

    Copy service = new Copy();
    CopySoap port = service.getCopySoap();

    BindingProvider bp = (BindingProvider) port;

    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, 
            "https://my.site.com/sites/mysite/_vti_bin/copy.asmx");


    return port;
}   

public static void createDocument(CopySoap port) {
    String url = "https://my.site.com/sites/mysite/Shared Documents/Temp Folder/test.txt";
    String sourceUrl = "C:\\CopyFile\\READ-ME.txt";     

    DestinationUrlCollection urls = new DestinationUrlCollection();
    urls.getString().add(url);

    byte[] content = IoUtil.getBytes(new File(sourceUrl));

    FieldInformation titleInfo = new FieldInformation ();
    titleInfo.setDisplayName("Title");
    titleInfo.setType(FieldType.TEXT);
    titleInfo.setValue("Test Doc");

    FieldInformationCollection infos = new FieldInformationCollection ();
    infos.getFieldInformation().add(titleInfo);

    CopyResultCollection results = new CopyResultCollection ();

    Holder<CopyResultCollection> resultHolder = new Holder<CopyResultCollection>(results);      

    Holder<Long> longHolder = new Holder<Long>(new Long(-1));       

    port.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);

}