如何在asp.net mvc应用程序中为APPS附加sharepoint所需的参数?

时间:2023-01-09 04:02:19

According to the documentation for sharepoint apps, we need to always append the SP Host web url, so that the app can get the context from the host web.

根据sharepoint应用程序的文档,我们需要始终附加SP主机Web URL,以便应用程序可以从主机Web获取上下文。

The Sharepoint Context provider and the token help class autoamtically detects this querystring value and creates the context.

Sharepoint Context提供程序和令牌帮助类自动检测此查询字符串值并创建上下文。

In my asp.net mvc sharepoint app, I have this code:

在我的asp.net mvc sharepoint应用程序中,我有以下代码:

 public ActionResult InstallDesignPackage()
{
    // Use TokenHelper to get the client context and Title property.
    // To access other properties, the app may need to request permissions
    // on the host web.
    var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
    // Publishing feature GUID to use the infrastructure for publishing
    Guid PublishingFeature = Guid.Parse("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
    // The site-relative URL of the design package to install.
    // This sandbox design package should be uploaded to a document library
    // for practical purpose this can be a configuration setting in web.config.
    string fileRelativePath = @"/sites/devsite/brand/Dev.wsp";
    //string fileUrl = @"https://xxx.sharepoint.com/_catalogs/solutions/LV%20Team%20Site%20Design%20Package-1.0.wsp";
    using (var clientContext = spContext.CreateUserClientContextForSPHost())
    {
        // Load the site context explicitly or while installing the API, the path for
        // the package will not be resolved.
        // If the package cannot be found, an exception is thrown.
        var site = clientContext.Site;
        clientContext.Load(site);
        clientContext.ExecuteQuery();
        // Validate whether the Publishing feature is active.
        if (Helpers.FeatureHelper.IsSiteFeatureActivated(clientContext, PublishingFeature))
        {
            DesignPackageInfo info = new DesignPackageInfo()
            {
                PackageGuid = Guid.Empty,
                MajorVersion = 1,
                MinorVersion = 1,
                PackageName = "Dev"
            };
            Console.WriteLine("Installing design package ");
            DesignPackage.Install(clientContext, clientContext.Site, info, fileRelativePath);
            clientContext.ExecuteQuery();
            Console.WriteLine("Applying design package");
            DesignPackage.Apply(clientContext, clientContext.Site, info);
            clientContext.ExecuteQuery();
        }
    }
    return View();
}

It just gets the not hidden sharepoint list from the host web.

它只是从主机网络获取未隐藏的共享点列表。

However, this is not the only page in my app of course, I want to have a navigation to other pages.

但是,这不是我应用程序中唯一的页面,我想要导航到其他页面。

In asp.net mvc I have seen they render the links like this:

在asp.net mvc我看到他们渲染这样的链接:

<div class="navbar">
    <div class="container">
        <ul class="nav navbar-nav">
            <li>@Html.ActionLink("About", "About", "Home")</li>
            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("Install Design Package", "InstallDesignPackage", "Home")</li>

        </ul>
    </div>
</div>

When you check the link this generates, it doesnt use the querystring SPHostUrl Check screenshot below, check the bottom of the screenshot for the link generated, you see it has no query string parameters, which means I wouldnt be able to get the sharepoint context tehre.

当你检查这个生成的链接时,它不会使用查询字符串SPHostUrl检查下面的截图,检查屏幕截图底部生成的链接,你看它没有查询字符串参数,这意味着我无法获得sharepoint context tehre 。

The question is: How can I render the links the correct way so that they always get the correct querystring parameters and values in all pages??

问题是:如何以正确的方式呈现链接,以便它们始终在所有页面中获得正确的查询字符串参数和值?

如何在asp.net mvc应用程序中为APPS附加sharepoint所需的参数?

1 个解决方案

#1


7  

After several days, this was pretty simple, but it was really hard to find a resource with the same problem I was having.

几天之后,这很简单,但很难找到一个与我遇到的问题相同的资源。

1st. The controller action needs to be surrounded with this attribute

1。控制器操作需要使用此属性包围

 [SharePointContextFilter]

2nd. The Action Links needs to be generated this way.

第2位。需要以这种方式生成Action Links。

<li>@Html.ActionLink("Install Design Package", "InstallDesignPackage", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri },null)</li>

If you feel like reading the entire story, I have documented it on my blog over here: http://levalencia-public.sharepoint.com/Pages/2014/03/Sharepoint-App---ASP-NET-MVC-Navigation-Link-with-correct-querystring-parameters.aspx

如果你想阅读整个故事,我已经在我的博客上记录了它:http://levalencia-public.sharepoint.com/Pages/2014/03/Sharepoint-App---ASP-NET-MVC-Navigation -Link-与纠正,查询字符串,parameters.aspx

#1


7  

After several days, this was pretty simple, but it was really hard to find a resource with the same problem I was having.

几天之后,这很简单,但很难找到一个与我遇到的问题相同的资源。

1st. The controller action needs to be surrounded with this attribute

1。控制器操作需要使用此属性包围

 [SharePointContextFilter]

2nd. The Action Links needs to be generated this way.

第2位。需要以这种方式生成Action Links。

<li>@Html.ActionLink("Install Design Package", "InstallDesignPackage", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri },null)</li>

If you feel like reading the entire story, I have documented it on my blog over here: http://levalencia-public.sharepoint.com/Pages/2014/03/Sharepoint-App---ASP-NET-MVC-Navigation-Link-with-correct-querystring-parameters.aspx

如果你想阅读整个故事,我已经在我的博客上记录了它:http://levalencia-public.sharepoint.com/Pages/2014/03/Sharepoint-App---ASP-NET-MVC-Navigation -Link-与纠正,查询字符串,parameters.aspx