简体   繁体   English

在 Java 中从 SharePoint 列表中删除文件

[英]Deleting File from SharePoint List in Java

I'm trying to delete a file from a Sharepoint list in Java and running into some issues.我正在尝试从 Java 中的 Sharepoint 列表中删除文件并遇到一些问题。 I'm using a batch element described here我正在使用此处描述的批处理元素

I'm able to make the request, but the results that come back are null and the file is not deleted (I don't get any errors).我能够提出请求,但返回的结果为空,并且文件没有被删除(我没有收到任何错误)。

Here is the code for the UpdateListItems.Update that I'm using:这是我正在使用的 UpdateListItems.Update 的代码:

    UpdateListItems.Updates updates = new UpdateListItems.Updates();
    updates.getContent().add(this.generateXmlNode(      
        "<Batch PreCalc='True' OnError='Continue' ListVersion='1' ListName='" + spMoveRequest.getListName() + "'>" +
            "<Method ID='1' Cmd='Delete'>" +
            "<Field Name='ID'>5</Field>" +//this must be where we   specify the file
            "</Method>" +
        "</Batch>"
    ));  

I'm then making a method call on the listSoap object like this:然后我对 listSoap 对象进行方法调用,如下所示:

    UpdateListItemsResult updateResult = listSoap.updateListItems("<my list name here>", updates);

I've also tried many variations like using the GUID instead of the actual list name and using我还尝试了许多变体,例如使用 GUID 而不是实际列表名称并使用

    <Field Name='FileRef'><my file url here></Field>

to identify the file.来识别文件。

Nothing seems to be working, and I'm not getting any useful feedback either.似乎没有任何效果,我也没有得到任何有用的反馈。

The generateXmlNode method that I'm using looks like this:我正在使用的 generateXmlNode 方法如下所示:

        protected Node generateXmlNode(String sXML) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document documentOptions = builder.parse(new InputSource(new StringReader(sXML)));
    Node elementOptions = documentOptions.getDocumentElement();
    return elementOptions;

}

but I've used this in the past when retrieving sharepoint lists without problems.但是我过去在检索共享点列表时没有问题地使用过它。

What am I missing here?我在这里缺少什么?

An other solution is to use HTTP DELETE method to delete a file directly to the Sharepoint with NTLMv2 authentication另一种解决方案是使用HTTP DELETE 方法将文件直接删除到具有 NTLMv2 身份验证的 Sharepoint

For that you can use Apache HTTP Client :为此,您可以使用Apache HTTP 客户端

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.3</version>
</dependency>

And to permit NTLMv2 authentication you need JCIF library.要允许 NTLMv2 身份验证,您需要JCIF库。

<dependency>
   <groupId>jcifs</groupId>
   <artifactId>jcifs</artifactId>
   <version>1.3.17</version>
</dependency>

First we need to write a wrapper to permit Apache HTTP Client to use JCIF for NTLMv2 support :首先,我们需要编写一个包装器来允许 Apache HTTP 客户端使用 JCIF 来支持 NTLMv2:

public final class JCIFSEngine implements NTLMEngine {

    private static final int TYPE_1_FLAGS =
            NtlmFlags.NTLMSSP_NEGOTIATE_56
            | NtlmFlags.NTLMSSP_NEGOTIATE_128
            | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2
            | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN
            | NtlmFlags.NTLMSSP_REQUEST_TARGET;

    @Override
    public String generateType1Msg(final String domain, final String workstation)
            throws NTLMEngineException {
        final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation);
        return Base64.encode(type1Message.toByteArray());
    }

    @Override
    public String generateType3Msg(final String username, final String password,
            final String domain, final String workstation, final String challenge)
            throws NTLMEngineException {
        Type2Message type2Message;
        try {
            type2Message = new Type2Message(Base64.decode(challenge));
        } catch (final IOException exception) {
            throw new NTLMEngineException("Invalid NTLM type 2 message", exception);
        }
        final int type2Flags = type2Message.getFlags();
        final int type3Flags = type2Flags
                & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));
        final Type3Message type3Message = new Type3Message(type2Message, password, domain,
                username, workstation, type3Flags);
        return Base64.encode(type3Message.toByteArray());
    }
}

Reference参考

The main code to execute HTTP DELETE with authentication:使用身份验证执行 HTTP DELETE 的主要代码:

    try {

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        //Register JCIF NTLMv2 to manage ntlm auth.
        httpclient.getAuthSchemes().register("ntlm", new AuthSchemeFactory() {
            @Override
            public AuthScheme newInstance(HttpParams hp) {
                return new NTLMScheme(new JCIFSEngine());
            }
        });

        //Provide login/password
        httpclient.getCredentialsProvider().setCredentials(
                AuthScope.ANY,
                new NTCredentials([LOGIN], [PASSWORD], "", [DOMAIN]));
        //Create HTTP PUT Request   

        HttpPut request = new HttpDelete("http://[server]/[site]/[folder]/[fileName]");         
        return httpclient.execute(request);

    } catch (IOException ex) {
      //...
    }

Linked question to upload a file to Sharepoint : How do I upload a document to SharePoint with Java?将文件上传到 Sharepoint 的链接问题: 如何使用 Java 将文档上传到 SharePoint?

You can take a look of this project i've developed to provide a working sharepoint rest api client usable in a very easy way.你可以看看我开发的这个项目,它提供了一个工作的 sharepoint rest api 客户端,可以以非常简单的方式使用。 Take a look in github here:看看这里的github:

https://github.com/kikovalle/PLGSharepointRestAPI-java https://github.com/kikovalle/PLGSharepointRestAPI-java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM