none
Querying AppWeb (remote web) URL from MVC controller in SharePoint Provider Hosted Add-In RRS feed

  • Question

  • Hi,

    In my SharePoint Provider Hosted Add-In built on Asp.Net MVC 5, I can get the HostWeb URL through query string.

    Could someone please help me to query the AppWeb (remote web) URL from controller?

    Thanks.

    Wednesday, October 17, 2018 3:41 AM

Answers

  • Hi,

    If you want to get the app web Url, the following code for your reference.

    var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
    var appwebUrl=spContext.SPAppWebUrl;

    If you want to get the current site URL, the following code for your reference.

    var currentWebUrl = string.Format("{0}://{1}{2}{3}",
    		  System.Web.HttpContext.Current.Request.Url.Scheme,
    		  System.Web.HttpContext.Current.Request.Url.Host,
    		  System.Web.HttpContext.Current.Request.Url.Port == 80 ? string.Empty : ":" + System.Web.HttpContext.Current.Request.Url.Port,
    		  System.Web.HttpContext.Current.Request.ApplicationPath);

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by pointtoshare Friday, October 19, 2018 10:30 AM
    Wednesday, October 17, 2018 9:49 AM
    Moderator

All replies

  • Hi,

    If you want to get the app web Url, the following code for your reference.

    var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
    var appwebUrl=spContext.SPAppWebUrl;

    If you want to get the current site URL, the following code for your reference.

    var currentWebUrl = string.Format("{0}://{1}{2}{3}",
    		  System.Web.HttpContext.Current.Request.Url.Scheme,
    		  System.Web.HttpContext.Current.Request.Url.Host,
    		  System.Web.HttpContext.Current.Request.Url.Port == 80 ? string.Empty : ":" + System.Web.HttpContext.Current.Request.Url.Port,
    		  System.Web.HttpContext.Current.Request.ApplicationPath);

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by pointtoshare Friday, October 19, 2018 10:30 AM
    Wednesday, October 17, 2018 9:49 AM
    Moderator
  • Hi Dennis,

    I got my answer.

    One additional request-

    Is there any way to get the website url? i.e. the app domain url where my app is being redirected to?

    Thanks. 

    Wednesday, October 17, 2018 10:13 AM
  • Hi,

    Can you provide an example for the " app domain url"?

    It is not mean "SPAppWebUrl"?

    Did you try to use the "currentWebUrl" in code above?

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, October 18, 2018 10:05 AM
    Moderator
  • Hi Dennis,

    Sorry, my all answers are here. Thanks a lot.

    Friday, October 19, 2018 10:30 AM