Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append sharepoint required parameters for APPS, in asp.net mvc apps?

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.

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

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

 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:

<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.

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??

enter image description here

like image 327
Luis Valencia Avatar asked Mar 08 '14 22:03

Luis Valencia


1 Answers

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

 [SharePointContextFilter]

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

<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

like image 87
Luis Valencia Avatar answered Sep 25 '22 08:09

Luis Valencia