3

The requirement is quite simple.

I developed an app, which shows the population, cpi, gdp of the specified country.
This app in sharepoint-hosted, and I'm using sharepoint online.
There are several country pages, such as spain.aspx, france.aspx, etc. The app is inserted to each page. When an user clicks on the link, the app is supposed to display the corresponding country's population, cpi, gdp data. But now I have no idea in the app how to get the country name(spain, france).

I tried window.location.pathname and document.referrer, but they all didn't work for me.

0

One possible workaround would be to use app part parameters. The downside is that you have to set such a parameter every time you add an app part to a new country page.

So e.g. you added the app part to france.aspx -> and now you have to configure this app part and set "Country" parameter to France.

If this is OK for you, the following MSDN article contains very good description of how to create app part and it's parameters:

In brief, you should do the following:

  1. Select your ClientWebPart element in project, right click, Properties
  2. Add element Country to CustomProperties collection and configure it to be either enum or string or whatever you prefer
  3. Use this parameter on your app part page

Visual Studio will generate Elements.xml based on the configuration you provided when defining custom property. This Elements.xml could look something like this:

<Content Type="html" Src="~appWebUrl/Pages/ClientWebPart1.aspx?{StandardTokens}&amp;Country=_Country_" />

<!-- Define properties in the Properties element.
     Remember to put Property Name on the Src attribute of the Content element above. -->
<Properties>
  <Property Name="Country" Type="enum" RequiresDesignerPermission="true" DefaultValue="none" WebDisplayName="Country">
    <EnumItems>
      <EnumItem Value="none" WebDisplayName="(not selected)" />
      <EnumItem Value="France" WebDisplayName="France" />
      <EnumItem Value="Spain" WebDisplayName="Spain" />
      <!-- ... other countries ... -->
    </EnumItems>
  </Property>
</Properties>

Then, you can use this parameter to display appropriate country information (or warning if it is not defined):

var country = '';
if (document.URL.indexOf('?') != -1) {
    var params = document.URL.split('?')[1].split('&');
    for (var i = 0; i < params.length; i++) {
        var p = decodeURIComponent(params[i]);
        if (/^Country=/i.test(p)) {
            country = p.split('=')[1];
            break;
        }
    }
}

if (country == null || country == "none") {
    document.write("COUNTRY PARAMETER IS NOT CONFIGURED! Please contact administrator.");
    return;
}

// retrieve country information based on country variable and display it

Finally, in the user interface it will look like this:

enter image description here

and if it is not configured:

enter image description here

  • Thanks a lot. In my site, there is no list for users to choose the country. There are three pages, /SitePages/france.aspx, /SitePages/spain.aspx, /SitePages/russia.aspx. How can the country be passed to client web part or app part? – wei Aug 28 '13 at 12:53
  • I'm proposing workaround. If you choose to go with it, you're supposed to create such a list, or just use string values. And you're supposed to configure each app part manually as you add it to a country page. Hey, I described all this in the post above... Don't jump to code & pictures right away, read explanations also ;) – Andrey Markeev Aug 28 '13 at 14:57
  • I've followed what you proposed. I'm new to sharepoint. Now I'm able to get the country value in the client web part. Another doubt is coming. How to pass the country in the url(spain.aspx, russia.aspx) to the client web part? – wei Aug 29 '13 at 13:34
  • Ok, I'll try one more time: so you have e.g. spain.aspx page. You're adding your app part there. After the app part is added, edit app part settings and manually set Country property of the app part to "Spain". Currently it's not possible to do this automatically. – Andrey Markeev Aug 29 '13 at 17:32
  • Please mark my answer as accepted if it answers your question. And upvote it if you want to express your thanks ;) – Andrey Markeev Sep 3 '13 at 11:45
1

According to this thread (particularly the answer from the PM on the Office Team), it doesn't appear that you can get the page URL from an App Part.

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/956d92fb-e0b3-4f32-9dae-c5ba5948949b/get-host-page-url-in-app-part

  • Thanks. Is there any way to work around it? I just need to get the country name from the page which contains the app. – wei Aug 27 '13 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.