Hi, I am developing a Provider-hosted app and i want to get the current user's department, picture url, etc. I am trying to accomplish the ff scenario by using Sharepoint API "_api/Web/SiteUserInfoList/" inside my MVC controller. Not sure if this is the best way to accomplish this. I want to pass all the result from the SP API into my viewbag so i can utilize them in my .cshtml files. All ViewBag declared in my code can be access in my .cshtml file with no problem just for testing purposes. However, when I add the
Stream s = request.GetResponse().GetResponseStream();
returns "An unexpected error has occurred.".
CODE: Inside controller
using ...
...
public ActionResult Index() {
...
var responseToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, targetWeb.Authority, targetRealm); ViewBag.resToken = responseToken; // HTTP Request and Response construction section HttpWebRequest request = HttpWebRequest.CreateHttp(String.Format("{0}/_api/Web/SiteUserInfoList/Items(14)", HttpContext.Request.QueryString["SPHostUrl"])); ViewBag.req = request; request.Method = "GET"; request.Accept = "application/json;odata=verbose"; request.ContentType = "application/json"; request.Headers.Add("Authorization", String.Format("{0} {1}", responseToken.TokenType, responseToken.AccessToken)); ViewBag.accessToken = responseToken.AccessToken; //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-- below line of code returns an error: Stream s = request.GetResponse().GetResponseStream();
}
Maybe someone can fill in the missing requirements/steps/codes or share me ways how to achieve this in some different ways.
Thanks,
Jeff