0

我在创建列表项时遇到了一点问题,其中“创建者”用户是正确的。我的场景是我通过 Visual Studio 部署了一个 Provider-Host 应用程序(以 user1 身份登录)并且一切正常。但是,如果我以其他用户 (user2) 的身份登录计算机,然后通过应用程序创建一个新的列表项,那么“创建者”用户是否设置为 user1?注意:我为此使用了 Rest API……我在下面粘贴了我的代码:

public void AddDocumentToLibrary(string documentName, string libraryName, string data, string sharePointUrl)
    {
        string newFormDigest = GetFormDigest(sharePointUrl);

        string digestRequest = sharePointUrl + "/_api/web/GetFolderByServerRelativeUrl('/" + libraryName + "')/Files/add(url='" + documentName + "', overwrite=true)";

        HttpWebRequest spNewRequest = (HttpWebRequest)HttpWebRequest.Create(digestRequest);
        CredentialCache credNewCache = new CredentialCache();
        credNewCache.Add(new Uri(digestRequest), "NTLM", CredentialCache.DefaultNetworkCredentials);
        spNewRequest.Credentials = credNewCache;
        spNewRequest.Method = "POST";
        spNewRequest.Accept = "application/json;odata=verbose";
        spNewRequest.ContentType = "application/json;odata=verbose";
        spNewRequest.Headers.Add("X-RequestDigest", newFormDigest);

        // For Content Length
        byte[] postByte = Encoding.UTF8.GetBytes(data);
        spNewRequest.ContentLength = postByte.Length;
        Stream postStreamBody = spNewRequest.GetRequestStream();
        postStreamBody.Write(postByte, 0, postByte.Length);
        postStreamBody.Close();

        HttpWebResponse webResponse = (HttpWebResponse)spNewRequest.GetResponse();
        GetHTTPResponse(webResponse);
    }

    private string GetContextInformation(string sharePointUrl)
    {
        // 1st request to get the context information
        string formdigestRequest = sharePointUrl + "/_api/contextinfo";
        HttpWebRequest spRequest = (HttpWebRequest)HttpWebRequest.Create(formdigestRequest);
        CredentialCache credNewCache = new CredentialCache();
        credNewCache.Add(new Uri(formdigestRequest), "NTLM", CredentialCache.DefaultNetworkCredentials);
        spRequest.Credentials = credNewCache;
        spRequest.Method = "POST";
        spRequest.Accept = "application/json;odata=verbose";
        spRequest.ContentLength = 0;
        HttpWebResponse endpointResponse = (HttpWebResponse)spRequest.GetResponse();
        string contextInformation = GetHTTPResponse(endpointResponse);

        return contextInformation;
    }

    private string GetFormDigest(string sharePointUrl)
    {
        string contextInformation = GetContextInformation(sharePointUrl);

        // Get the FormDigest Value
        var startTag = "FormDigestValue";
        var endTag = "LibraryVersion";
        var startTagIndex = contextInformation.IndexOf(startTag) + 1;
        var endTagIndex = contextInformation.IndexOf(endTag, startTagIndex);
        string newFormDigest = null;
        if ((startTagIndex >= 0) && (endTagIndex > startTagIndex))
        {
            newFormDigest = contextInformation.Substring(startTagIndex + startTag.Length + 2, endTagIndex - startTagIndex - startTag.Length - 5);
        }

        return newFormDigest;
    }

    private string GetHTTPResponse(HttpWebResponse endpointResponse)
    {
        Stream postStream = endpointResponse.GetResponseStream();
        StreamReader postReader = new StreamReader(postStream);

        string results = postReader.ReadToEnd();

        return results;
    }

对此的任何见解都会很棒。如果我有点模糊,我也可以尝试解释一下。

在此先感谢,大卫

4

1 回答 1

0

我已经解决了。出于某种原因,上面的代码似乎会使用首先安装该应用程序的用户帐户......至少它似乎为我做的。

我稍微更改了代码,因此我传入了您可以从 SharePointContext [SharePointContextProvider.Current.GetSharePointContext(HttpContext)] 获取的“UserAccessTokenForSPHost”值,这将检索当前登录用户的用户访问令牌。

然后,我们可以在创建请求对象时使用此令牌以登录用户的身份与 SharePoint 进行通信。主要是,我更新了 AddDocumentToLibrary 函数

public void AddDocumentToLibrary(string documentName, string libraryName, string data, string sharePointUrl)
{
    string newFormDigest = GetFormDigest(sharePointUrl);

    string digestRequest = sharePointUrl + "/_api/web/GetFolderByServerRelativeUrl('/" + libraryName + "')/Files/add(url='" + documentName + "', overwrite=true)";

    HttpWebRequest spNewRequest = (HttpWebRequest)HttpWebRequest.Create(digestRequest);
    CredentialCache credNewCache = new CredentialCache();
    credNewCache.Add(new Uri(digestRequest), "NTLM", CredentialCache.DefaultNetworkCredentials);
    spNewRequest.Credentials = credNewCache;
    spNewRequest.Method = "POST";
    spNewRequest.Accept = "application/json;odata=verbose";
    spNewRequest.ContentType = "application/json;odata=verbose";
    spNewRequest.Headers.Add("X-RequestDigest", newFormDigest);

    // For Content Length
    byte[] postByte = Encoding.UTF8.GetBytes(data);
    spNewRequest.ContentLength = postByte.Length;
    Stream postStreamBody = spNewRequest.GetRequestStream();
    postStreamBody.Write(postByte, 0, postByte.Length);
    postStreamBody.Close();

    HttpWebResponse webResponse = (HttpWebResponse)spNewRequest.GetResponse();
    GetHTTPResponse(webResponse);
}

成为:

public void AddDocumentToLibrary(string documentName, string libraryName, string data, string sharePointUrl, string userAccessToken)
{
    string newFormDigest = GetFormDigest(sharePointUrl);

    string digestRequest = sharePointUrl + "/_api/web/GetFolderByServerRelativeUrl('/" + libraryName + "')/Files/add(url='" + documentName + "', overwrite=true)";

    HttpWebRequest spNewRequest = (HttpWebRequest)HttpWebRequest.Create(digestRequest);
    spNewRequest.Headers.Add("Authorization", "Bearer " + userAccessToken);            
    spNewRequest.Method = "POST";
    spNewRequest.Accept = "application/json;odata=verbose";
    spNewRequest.ContentType = "application/json;odata=verbose";
    spNewRequest.Headers.Add("X-RequestDigest", newFormDigest);

    // For Content Length
    byte[] postByte = Encoding.UTF8.GetBytes(data);
    spNewRequest.ContentLength = postByte.Length;
    Stream postStreamBody = spNewRequest.GetRequestStream();
    postStreamBody.Write(postByte, 0, postByte.Length);
    postStreamBody.Close();

    HttpWebResponse webResponse = (HttpWebResponse)spNewRequest.GetResponse();
    GetHTTPResponse(webResponse);
}

感谢上帝,这现在有效。

于 2014-04-11T07:22:20.137 回答