Code: Select all
public async Task AddFolderAsync(string library, string folderName)
{
var _serverRelativeUrl = string.Join("/", new string[] { library, folderName });
var _type = "SP.Folder";
var kvPropertyValues = new List
{
new KeyValuePair(ServerRelativeUrl, _serverRelativeUrl)
};
string json = GetJsonMetadataStringByType(_type, kvPropertyValues);
// this returns: '{'__metadata': {'type': 'SP.Folder'},'ServerRelativeUrl':'TEST/'}'
const string urlTemplate = "{0}/_api/web/folders";
var restUrl = string.Format(urlTemplate, webUrl);
var endpoint = _authenticationManager.GetWebRequest(restUrl, HttpMethod.Post, null, false);
using Stream requestStream = endpoint.GetRequestStream();
using StreamWriter writer = new(requestStream);
{
writer.Write(json);
writer.Flush();
}
HttpWebResponse httpResponse = await GetResponse(endpoint);
// Further stuff to be done here...
}
Code: Select all
{'__metadata': {'type': 'SP.Folder'},'ServerRelativeUrl':'TEST/'}
< /code>
Dies stimmt mit den hier gefundenen Anweisungen aus:
Microsoft - SharePoint Rest -API < /p>
Dies funktioniert brillant, die GetWebRequest < / Code> Call bevölkert die Header und gibt die Anfrage zurück. Dies funktioniert alles gut. Die WebRequest -Methode sieht folgendermaßen aus: < /p>
public HttpWebRequest GetWebRequest(string url, HttpMethod method, string xHttpMethod, bool noMetaData)
{
try
{
var accessToken = EnsureAccessTokenAsync(siteURI, user, password).GetAwaiter().GetResult();
var odata = noMetaData ? OData.NOMETADATA : OData.VERBOSE;
var endpointRequest = HttpWebRequest.CreateHttp(url);
endpointRequest.Method = method.ToString();
endpointRequest.Accept = $"application/json;odata={odata}";
endpointRequest.ContentType = $"application/json;odata={odata}";
endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
if (!string.IsNullOrEmpty(xHttpMethod))
{
endpointRequest.Headers.Add("IF-MATCH", "*");
endpointRequest.Headers.Add("X-HTTP-Method", xHttpMethod.ToUpper());
}
return endpointRequest;
}
catch (System.Exception)
{
throw;
}
}
d. H. < /p>
Code: Select all
var sc = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
var _httpResponse = await _authenticationManager.GetHttpResponseFromRequestWithBody(restUrl, HttpMethod.Post, sc, null);
Code: Select all
private HttpRequestMessage GetHttpRequestMessage(string url, HttpMethod method, string xHttpMethod, string odata)
{
var request = new HttpRequestMessage(method, url);
// Set Bearer accessToken
var accessToken = EnsureAccessTokenAsync(siteURI, user, password).GetAwaiter().GetResult();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Set AcceptHeader
var acceptHeader = MediaTypeWithQualityHeaderValue.Parse($"application/json;odata={odata}");
request.Headers.Accept.Add(acceptHeader);
// IF-Match header
if (!string.IsNullOrEmpty(xHttpMethod))
{
// request.Headers.Add("IF-MATCH", "*");
request.Headers.IfMatch.Add(new EntityTagHeaderValue("*"));
request.Headers.Add("X-HTTP-Method", xHttpMethod.ToUpper());
}
return request;
}
public Task GetHttpResponseFromRequestWithBody(string url, HttpMethod method, HttpContent content, string xHttpMethod, string odata = OData.NOMETADATA)
{
var request = GetHttpRequestMessage(url, method, xHttpMethod, odata);
request.Content = content;
Debug.WriteLine(request.Content);
return httpClient.SendAsync(request);
}
Mobile version