0

I am developing a Provider hosted app of MVC type for SharePoint Online. I am trying to get the SharePoint Context in a Web Method in provider Hosted app of Type MVC application. I am able to get the context in the Index method.

[SharePointContextFilter]
        public ActionResult Index()
        {
            User spUser = null;
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    // objClientContext = clientContext;
                    spUser = clientContext.Web.CurrentUser;
                    clientContext.Load(spUser, user => user.Title);
                    clientContext.ExecuteQuery();
                    ViewBag.UserName = spUser.Title;
                }
            }
            return View();
        }

But i am not able to get the same context in the Web Method.

  [WebMethod]
        public JsonResult GetSiteWithContext()
        {
            User spUser = null;
            SiteCollection objsiteColl = new SiteCollection();
            try
            {
                var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    spUser = clientContext.Web.CurrentUser;
                    clientContext.Load(spUser, user => user.Title);
                    clientContext.ExecuteQuery();
                    objsiteColl.Title = spUser.Title;
                    //  return Json((new { objsiteColl }), JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {

            }
            return Json(objsiteColl, JsonRequestBehavior.AllowGet);
        }

I am getting the error object reference not set to an instance of an object at the line using (var clientContext = spContext.CreateUserClientContextForSPHost())

If i write a Web method without using the context it working fine.

 [WebMethod]
        public JsonResult GetSiteWithoutContext()
        {
            Name objName = new Name();
            objName.SiteName = "DEMO";
            return Json(objName, JsonRequestBehavior.AllowGet);
            //  return Json((new { objName }), JsonRequestBehavior.AllowGet);

        }

Can anyone please suggest how to get the SharePoint Context in Web Methods.

0

You don't need the WebMethod. Any MVC controller methods can be called via AJAX. so remove WebMethod from your method.

public JsonResult GetSiteWithoutContext() { -- your logic return Json(objName, JsonRequestBehavior.AllowGet); }

then use ajax call from your Javascript method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.