[RESOLVED] SiteMapNode and ExplicitResourceKeys

has anyone ever use the SiteMap node constructor that takes ExplicitResourceKeys parameter?

I dont find any detail documentation by microsoft on localizing sitemap. I know how to localize XmlSiteMapProvider. But im using SqlSiteMapProvider and i want to localize it. One of the parameter of SiteMapNode takes ExplicitResourceKeys. But i dont know what to pass there. Its a namevalue collection type

Anyone knows? my resources are also stored in SQL and i have custom SQLResourceProvider which is working  fine with it.

never mind i solved it

Can you tell us how did you to solve this problem?

I’m using SQL site map provider from http://msdn.microsoft.com/en-us/magazine/cc163657.aspx  I have added one more column in [SiteMap] table to store resource key for the Title. If you want to localize description also then you have to add one more column for description resource key. Then i changed CreateSiteMapNodeFromDataReader() method to build explicitResourceKey collection. I’m also using SQL Resource provider from http://msdn.microsoft.com/en-us/library/aa905797.aspx 

IMPORTANT: I’m using SiteMap provider for Menu control which will be on masterpage.So all my SiteMap resources are storeded in SQL database as Global resources. [StringResources] table has column called “ResourceType”. This column’s value will be common to all SiteMap related resources. This is the class name that we have use to while building explicitResourceKey collection. I used some constant value. Eg “SqlSiteMapProvider“

 

private const string ClassName = "SqlSiteMapProvider";

private SiteMapNode CreateSiteMapNodeFromDataReader(DbDataReader reader)
{

            // Make sure the node ID is present

            if (reader.IsDBNull(_indexID))

                throw new ProviderException(_errmsg1);

            // Get the node ID from the DataReader

            int id = reader.GetInt32(_indexID);

            // Make sure the node ID is unique

            if (_nodes.ContainsKey(id))

                throw new ProviderException(_errmsg2);


            // Get title, URL, description, and roles from the DataReader

            string title = reader.IsDBNull(_indexTitle) ?
                null : reader.GetString(_indexTitle).Trim();
            string url = reader.IsDBNull(_indexUrl) ?
                null : reader.GetString(_indexUrl).Trim();
            string description = reader.IsDBNull(_indexDesc) ?
                null : reader.GetString(_indexDesc).Trim();
            string roles = reader.IsDBNull(_indexRoles) ?
                null : reader.GetString(_indexRoles).Trim();
            string resourcekey = reader.IsDBNull(_indexResourceKey) ?
                null : reader.GetString(_indexResourceKey).Trim();


            // If roles were specified, turn the list into a string array

            string[] rolelist = null;
            if (!String.IsNullOrEmpty(roles))
                rolelist = roles.Split(new char[] { ',', ';' }, 512);
            SiteMapNode node = null;
            //if node has localize value in database then get that
            //else use the defualt one

            if (EnableLocalization && !string.IsNullOrEmpty(resourcekey))
            {
                NameValueCollection keyCollection = new NameValueCollection();
                keyCollection.Add("Title", ClassName);
                keyCollection.Add("Title", resourcekey);
                node = new SiteMapNode(this, id.ToString(), url,
                    title, description, rolelist, null, keyCollection, null);

                //TO DO: we can also localize description property
                //keyCollection.Add("Description", ClassName);
                //keyCollection.Add("Description", descriptionresourcekey);

            }
            else
            {
                node = new SiteMapNode(this, id.ToString(), url,
                            title, description, rolelist, null, null, null);
            }          

            // Record the node in the _nodes dictionary
            _nodes.Add(id, node);

            // Return the node
            return node;
        }



 

Thanks for sharing!

I think many others will benefit from it.

lax4u

IMPORTANT: I’m using SiteMap provider for Menu control which will be on masterpage.So all my SiteMap resources are storeded in SQL database as Global resources. [StringResources] table has column called “ResourceType”. This column’s value will be common to all SiteMap related resources. This is the class name that we have use to while building explicitResourceKey collection. I used some constant value. Eg “SqlSiteMapProvider“ 

I have get the SQL Site map work alone, the Resource Provider alone, but combined i can't get it working. it doesn't get localized. Debugging in your added code shows that it actually adds the keyColelction, so I guess it has to do with my items in the database.

If in the Sitemap table my item have the value of the column "resourcekey" to "Sitemap_Home" and I haven't changed the "Title" in your code.
Should it be my classname, like your "SqlSiteMapProvider" or anything else as ResourceType?
Should it be "Sitemap_Home" or "Sitemap_Home.Title" in the resourcekey in StingResourcetable? 

 

Greateful for help.

Regards Pelle

@lax4u

Would you mind posting your complete solution (generic-ized of course) for the benefit of those who are... eh hem... less savvy.  You're doing exactly what I'm hoping to accomplish.

Thanks!

thanks for your sharring i try also create static site map driving from database and also i need for menu items localization i created a resource file for resources to get the data from there and also created some nameValueCollection for createNode() method but it doesn't work all i see is the titel value if you can please let me know how you change the enabel localization value to true and if you can please give us more information thanks

thoughts on "[RESOLVED] SiteMapNode and ExplicitResourceKeys"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>