0

「作成者」ユーザーが正しいリスト項目の作成に少し問題があります。私が持っているシナリオは、Visual Studio (user1 としてログインしている間) を介して Provider-Host アプリをデプロイし、すべてが正常に機能していることです。ただし、別のユーザー (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