我在创建列表项时遇到了一点问题,其中“创建者”用户是正确的。我的场景是我通过 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;
}
对此的任何见解都会很棒。如果我有点模糊,我也可以尝试解释一下。
在此先感谢,大卫