I'm trying to dynamically generate the WebUrl for a DataFormWebPart so that it uses the parent URL instead of a hardcoded value.

<WebPartPages:DataFormParameter Name="WebURL" ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/sites/test/"/>

I can't use / as the WebUrl as the parent site is located at /site/test/, but this value could change depending on where the subsite is deployed.

The question is, can this DefaultValue be dynamically generated? I tried DefaultValue="../" but that doesn't seem to work.

  • Tecknix, how do you deploy the DataViewWebPart to a site? – Andrey Markeev Jan 14 '12 at 11:03

Assuming you're deploying the DataFormWebPart through Elements.xml and <AllUserWebParts> element, you can use ~sitecollection and ~site tokens in order to get current site collection and current site URLs respectively. You can find some details and example code on this here.

Another approach could be to fix WebUrl parameter default value in FeatureActivated receiver programmatically. For this, you should instantiate SPLimitedWebPartManager for your page in the feature receiver, find your webpart, and retrieve SPDataSource from DataFormWebPart.DataSources property, and then update the DataFormParameter objects from SelectParameters, UpdateParameters, DeleteParameters and InsertParameters collections.

  • Unfortunately I am not deploying it through Elements.xml, but rather through a simple site template - I will checkout the example code though for possible suggestions. Thanks – TeckniX Jan 16 '12 at 16:51
  • 1
    Answer should be same. By the way, I've just found a blog post from Waldek Mastykarz on this topic. Apparently, you have the same problem: blog.mastykarz.nl/… – Andrey Markeev Jan 16 '12 at 18:27
  • Yup - exactly the same issue - I'll have to see if the Site actually works - I had tried Sitecollection, which failed. Thanks Omlin! – TeckniX Jan 23 '12 at 15:24

not sure how your implementing your web part, but this will give you the current site url:

    public string getCurrentSite()
        string currentSite = SPContext.Current.Web.Url;
        return currentSite;

also, you might want to check into this: http://philwicklund.com/Lists/Posts/Post.aspx?ID=5

  • Although this is in a way the correct solution, my sharepoint deployment doesn't allow any code behind or c# - I get the code block not allowed :\ I'm stuck with xslt and built-in asp variables. Thanks for the tip though. – TeckniX Jan 16 '12 at 16: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.