- private string GetFormDigestValue()
- {
- string digest = null;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.baseUrl + "/_api/contextinfo");
- request.Method = "POST";
- request.ContentType = "application/json;odata=verbose";
- request.UseDefaultCredentials = true;
- var postBody = string.Empty;
- request.ContentLength = postBody.Length;
- byte[] postData = Encoding.ASCII.GetBytes(postBody);
- using (Stream requestStream = request.GetRequestStream())
- {
- requestStream.Write(postData, 0, postData.Length);
- requestStream.Close();
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- using (Stream responseStream = response.GetResponseStream())
- {
- var encoding = ASCIIEncoding.ASCII;
- using (var reader = new StreamReader(response.GetResponseStream(), encoding))
- {
- // parse the ContextInfo response
- var resultXml = XDocument.Parse(reader.ReadToEnd());
- // get the form digest value
- var d = from e in resultXml.Descendants()
- where e.Name == XName.Get("FormDigestValue", "http://schemas.microsoft.com/ado/2007/08/dataservices")
- select e;
- digest = d.First().Value;
- }
- }
- }
- return digest;
- }
The POST requests will be prepared by the PreparePostRequest method before execution:
- private HttpWebRequest PreparePostRequest(string requestPath, string formDigestValue, string postBody)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.baseUrl + requestPath);
- request.Method = "POST";
- request.ContentType = "application/json;odata=verbose";
- request.Accept = "application/json;odata=verbose";
- request.Headers.Add("X-RequestDigest", formDigestValue);
- request.UseDefaultCredentials = true;
- request.ContentLength = postBody.Length;
- byte[] postData = Encoding.ASCII.GetBytes(postBody);
- System.IO.Stream requestStream = request.GetRequestStream();
- requestStream.Write(postData, 0, postData.Length);
- requestStream.Close();
- return request;
- }
The ExecuteAuthorizedPostRequest gets the form digest and prepares the request using the other two methods, finally sends the request and reads the response returned by the server.
- private HttpWebResponse ExecuteAuthorizedPostRequest(string requestPath, string postBody)
- {
- string formDigestValue = GetFormDigestValue();
- HttpWebRequest request = PreparePostRequest(requestPath, formDigestValue, postBody);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- return response;
- }
- public void CreateEnterpriseResource()
- {
- string requestPath = "/_api/ProjectServer/EnterpriseResources";
- // creating a new resource setting only its name
- //string postBody = "{ '__metadata': { 'type': 'PS.EnterpriseResource' }, 'Name':'New Resource' }";
- // creating a new resource setting its name and type ('Material' in this case)
- //string postBody = string.Format("{{ '__metadata': {{ 'type': 'PS.EnterpriseResource' }}, 'Name':'New Material Resource', 'ResourceType': '{0}' }}", (int)EnterpriseResourceType.Material);
- // creating a new resource setting its name and cost center
- string postBody = string.Format("{{ '__metadata': {{ 'type': 'PS.EnterpriseResource' }}, 'Name':'New CC Resource', 'CostCenter': '{0}' }}", "Your Cost Center");
- var resp = ExecuteAuthorizedPostRequest(requestPath, postBody);
- }
No comments:
Post a Comment