Хостинг надстройки Sharepoint и аутентификация приложения проекта - PullRequest
0 голосов
/ 14 февраля 2020

У меня есть хостинг надстройки Sharepoint, и я пытаюсь получить все проекты (из приложения Project) зарегистрированного пользователя.

Мой код:

public ActionResult Index()
    {
        User spUser = null;

        SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
        using (ClientContext clientContext = spContext.CreateUserClientContextForSPHost())
        {
            if (clientContext != null)
            {
                spUser = clientContext.Web.CurrentUser;

                clientContext.Load(clientContext.Web.CurrentUser);

                clientContext.ExecuteQuery();

                ViewBag.UserName = spUser.Title;
                ViewBag.ContractProcessCode = SharePointContext.GetContractProcessCode(HttpContext.Request);


                ProjectServer projectServer = new ProjectServer(clientContext);

                var allprojects = projectServer.Projects;

                clientContext.Load(allprojects);

                clientContext.ExecuteQuery();
            }
        }

        return View();
    }

Я создаю экземпляр ProjectServer с использованием clientContext (ProjectServer projectServer = new ProjectServer (clientContext);), но когда я выполняюQuery, я не получаю никакого проекта.

Но если я использую следующий код, он уже работает (пользователь то же самое):

public ActionResult Index()
    {
        User spUser = null;

        SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
        using (ClientContext clientContext = spContext.CreateUserClientContextForSPHost())
        {
            if (clientContext != null)
            {
                spUser = clientContext.Web.CurrentUser;

                clientContext.Load(clientContext.Web.CurrentUser);

                clientContext.ExecuteQuery();

                ViewBag.UserName = spUser.Title;
                ViewBag.ContractProcessCode = SharePointContext.GetContractProcessCode(HttpContext.Request);


                //ProjectServer projectServer = new ProjectServer(clientContext);

                //var allprojects = projectServer.Projects;

                //clientContext.Load(allprojects);

                //clientContext.ExecuteQuery();



                ProjectContext projContext = new ProjectContext("PWAUrl");
                projContext.Credentials = new NetworkCredential("Username", "Password");

                var allprojects = projContext.Projects;

                projContext.Load(allprojects);

                projContext.ExecuteQuery();
            }
        }

        return View();
    }

Кто-нибудь знает, как можно получить все проекты зарегистрированного пользователя?

Обновление: разрешено

Решение:

Код :

public ActionResult Index()
    {
        User spUser = null;

        SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
        using (ClientContext clientContext = spContext.CreateUserClientContextForSPHost())
        {
            if (clientContext != null)
            {
                spUser = clientContext.Web.CurrentUser;

                clientContext.Load(clientContext.Web.CurrentUser);

                clientContext.ExecuteQuery();

                ViewBag.UserName = spUser.Title;
                ViewBag.ContractProcessCode = SharePointContext.GetContractProcessCode(HttpContext.Request);


                ProjectServer projectServer = new ProjectServer(clientContext);

                var allprojects = projectServer.Projects;

                clientContext.Load(allprojects);

                clientContext.ExecuteQuery();
            }
        }

        return View();
    }

И нам нужно дать следующие разрешения: enter image description here

...