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

Lui*_*cia 5 c# asp.net-mvc sharepoint sharepoint-apps

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

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

在我的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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

在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>
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

Lui*_*cia 7

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

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

 [SharePointContextFilter]
Run Code Online (Sandbox Code Playgroud)

第2位.Action链接需要以这种方式生成.

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

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