SharePoint 2013提供商主机应用程序-创建列表项(Rest API)

SharePoint 2013提供商主机应用程序-创建列表项(Rest API),rest,sharepoint,sharepoint-apps,Rest,Sharepoint,Sharepoint Apps,我在创建一个列表项时遇到了一个小问题,用户创建的列表项就是正确的列表项。我的场景是,我在以user1身份登录时通过VisualStudio部署了一个提供商主机应用程序,一切正常。但是,如果我以其他用户user2的身份登录到计算机,然后通过应用程序创建一个新的列表项,则用户创建的列表项将设置为user1?注意:我正在为此使用RESTAPI。。。我已将代码粘贴到下面: public void AddDocumentToLibrary(string documentName, string libra

我在创建一个列表项时遇到了一个小问题,用户创建的列表项就是正确的列表项。我的场景是,我在以user1身份登录时通过VisualStudio部署了一个提供商主机应用程序,一切正常。但是,如果我以其他用户user2的身份登录到计算机,然后通过应用程序创建一个新的列表项,则用户创建的列表项将设置为user1?注意:我正在为此使用RESTAPI。。。我已将代码粘贴到下面:

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;
    }
对这一点的任何洞察都将是伟大的。如果我有点含糊不清,我也可以试着解释一下

提前感谢,,
大卫

我已经解决了。出于某种原因,上面的代码所做的似乎是它将使用安装该应用程序的人的用户帐户。。。至少这看起来对我来说是这样的

我对代码进行了轻微修改,因此我将传入UserAccessTokenForSPHost值,您可以从SharePointContext[SharePointContextProvider.Current.GetSharePointContextHttpContext]获取该值,这将检索当前登录用户的用户访问令牌

然后,我们可以在创建请求对象以作为登录用户与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);
}
谢天谢地,这现在起作用了