我在使用"创建一个列表项时遇到了一些问题。用户是正确的。我的方案是我已经通过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;
}
对此的任何见解都会很棒。如果我有点模糊,我可以尝试解释一下这一点。
提前致谢, 大卫
答案 0 :(得分:0)
我已经解决了。出于某种原因,上面的代码似乎是在使用首先安装应用程序的用户帐户...至少它看起来像是在做什么。
我已经改变了代码,所以我传递了#User UserTccessTokenForSPHost"" UserAccessTokenForSPHost"您可以从SharePointContext [SharePointContextProvider.Current.GetSharePointContext(HttpContext)]获取的值,这将检索当前登录用户的用户访问令牌。
然后,我们可以在创建请求对象时使用此令牌,以便以登录用户身份与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);
}
谢天谢地,现在就可以了。