1

I've created a page layout which has a content by query web part embedded in it. I would like to have the CQWP pre-configured as much as possible so that the user doesn't need to make changes after page creation.

I've ran into this issue:

For the Source, I'd like to Show items from the following site and all subsites: and instead of placing /en/someSite, I'd like this value to be dynamic. It could be /fr/someSite if I'm in the French variation. I've thought about storing this in the resource files, but I'm not sure if and how I can pass $Resources:Some_Resource,Key as a parameter.

Basically, if I were to set up the web part XML, instead of

<property name="WebUrl" type="string">~sitecollection/en/someSite</property>

it would be something like

<property name="WebUrl" type="string">$Resource:Some_Resource,Key</property>

Is this possible? If not, what would be an alternate way to do this?

1
+50

Unfortunately just specifying resource string for WebUrl property of ContentByQueryWebPart web part is not enough since resource string could not be resolved dynamically (basically not supported) using SharePoint web parts infrastructure like in case of Url tokens (e.g. ~sitecollection).

But, you could consider the following approach.

Solution

The solution demonstrates how to dynamically resolve resource string specified for WebUrl property of CBQ web part, it consists of:

  1. a custom module (see WebPartsResourceManager class below) that
    allows to substitute the resource key with actual value for web part property when the web part is initialized
  2. in order to register that module,the custom publishing page layout is intended (see the PublishingLayout class below)

Module for to resolving web part property values from resource strings:

public class WebPartsResourceManager
{
    static WebPartsResourceManager()
    {
    }

    public static void Register(Page page)
    {
        page.PreRender += OnPagePreRender;
    }

    static void OnPagePreRender(object sender, EventArgs e)
    {
        using (new SPMonitoredScope("ResourceManagerWebPart.OnPreRender"))
        {
            try
            {
                var page = sender as Page;
                var manager = WebPartManager.GetCurrentWebPartManager(page);
                if (manager == null || manager.DisplayMode == WebPartManager.EditDisplayMode)
                {
                    return;
                }
                ForEachWebPart<ContentByQueryWebPart>(manager, ResolveWebUrlProperty);
            }
            catch (Exception ex)
            {
                //Logging goes here..
            }
        }

    }

    private static void ResolveWebUrlProperty(ContentByQueryWebPart cbq)
    {
        var lcid = System.Threading.Thread.CurrentThread.CurrentUICulture.LCID;
        cbq.WebUrl = SPUtility.GetLocalizedString("$Resources:Contoso.SharePoint,NewsSourceUrl;", "Contoso.SharePoint", (uint)lcid);
    }

    private static void ForEachWebPart<T>(WebPartManager manager, Action<T> action) where T : WebPart
    {
        var webParts = manager.WebParts.OfType<T>();
        foreach (var webPart in webParts)
        {
            action(webPart);
        }
    }
}

Custom Publishing Layout Page that allows to resolve WebUrl property from resource string:

public class PublishingLayout : PublishingLayoutPage
{

    public PublishingLayout()
        :base()
    {
        WebPartsResourceManager.Register(this);
    }
}

Now, you could create your page layout that inherits from PublishingLayout, for example:

<%@ Page Language="C#" Inherits="Contoso.Intranet.SharePoint.UI.ApplicationPages.PublishingLayout, Contoso.Intranet.SharePoint.UI, Version=1.0.0.0,Culture=neutral,PublicKeyToken=c5dbb8c5770bc890" %>

and specify WebUrl property like this:

<property name="WebUrl" type="string">$Resources:Contoso.SharePoint,NewsSourceUrl;</property> 

where NewsSourceUrl is a key from resource file (Contoso.SharePoint.resx)

1

You can create new feaure and in it's "FeatureReceiver.FeatureActivated" you can get web part on the page and change it with your resource file.

Look these two relative questions:

  1. How to localize webpart title, description, group, quickadd group in Sandbox solution
  2. CQWP - dynamically setting the list

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.