0

I'm using Sitecore 8.0 Update 5 and following this document to change the language name in URL.

The current URL is like http://mysite/fr-ca/products and I want it to be http://mysite/fr/products

Code:

namespace MySite.Foundation.Common.Utilities
{
    public class SitecoreExtensions
    {
        public class MySiteLinkProvider : LinkProvider
        {
            public override string GetItemUrl(Item item, UrlOptions options)
            {
                options.LanguageEmbedding = LanguageEmbedding.Always;
                options.LanguageLocation = LanguageLocation.FilePath;
                var languageId = LanguageManager.GetLanguageItemId(item.Language, item.Database);
                var languageItem = item.Database.GetItem(languageId);
                var regionName = languageItem["Region Name"] ?? "";
                var returnUrl = base.GetItemUrl(item, options).ReplaceFirst(item.Language.Name, regionName);
                return returnUrl;
            }
        }


        public class MySiteHttpRequestProcessor : HttpRequestProcessor
        {
            public override void Process(HttpRequestArgs args)
            {
                Assert.ArgumentNotNull(args, "args");

                // If Sitecore has already mapped the item, just return.
                if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0) return;

                foreach (var language in LanguageManager.GetLanguages(Context.Database))
                {
                    var languageId = LanguageManager.GetLanguageItemId(language, Context.Database);
                    var languageItem = Context.Database.GetItem(languageId);
                    var regionName = languageItem["Region Name"] ?? "";

                    if (args.Url.ItemPath.StartsWith("/" + regionName))
                    {
                        Context.SetLanguage(language, true);
                        Context.Item = ItemManager.GetItem(args.Url.ItemPath.ReplaceFirst("/" + regionName, ""), language, Version.Latest, Context.Database);
                        break;
                    }
                }                
            }
        }
    }
}

web.config

<linkManager defaultProvider="sitecore">
      <providers>
        <clear />
        <!--<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="always" languageLocation="filePath" lowercaseUrls="false" shortenUrls="true" useDisplayName="false" />-->
        <add name="sitecore" type="MySite.Foundation.Common.Utilities.SitecoreExtensions.MySiteLinkProvider, MySite.Foundation.Common" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="always" languageLocation="filePath" lowercaseUrls="false" shortenUrls="true" useDisplayName="false" />
      </providers>
    </linkManager>

MySite.config in the include folder:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
     <pipelines>
      <httpRequestBegin>
       <processor type="MySite.Foundation.Common.Utilities.SitecoreExtensions.MySiteHttpRequestProcessor, MySite.Foundation.Common" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" />
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

After publishing the project and browsing the page, it gives the error:

Could not resolve type name: MySite.Foundation.Common.Utilities.SitecoreExtensions.MySiteLinkProvider, MySite.Foundation.Common (method: Sitecore.Configuration.Factory.CreateFromTypeName(XmlNode configNode, String[] parameters, Boolean assert)).

UPDATE:

I commented the providers in web.config, and now the error in Sitecore client url is

Could not resolve type name: MySite.Foundation.Common.Utilities.SitecoreExtensions.MySiteHttpRequestProcessor, MySite.Foundation.Common (method: Sitecore.Configuration.Factory.CreateFromTypeName(XmlNode configNode, String[] parameters, Boolean assert)).

Is this the correct way to patch Handlers, providers or is there some other setting to be done prior to this.

3
  • You didn't deploy your code
    – Mark Cassidy
    Mar 13 '17 at 11:32
  • @MarkCassidy I did. There is one project (MySite.Foundation.Common) that has the code and the config. I published the project.
    – Qwerty
    Mar 13 '17 at 11:33
  • 2
    A wild guess, but did you try without nested classes? Remove "public class SitecoreExtensions" - don't forget to alter the configs as well.
    – Gatogordo
    Mar 13 '17 at 12:26
2

@gator is right, your class is nested.

So to instantiate it, your config would need to look like this:

<add name="sitecore"
     type="MySite.Foundation.Common.Utilities.SitecoreExtensions+MySiteLinkProvider, MySite.Foundation.Common" 
     addAspxExtension="false" 
     alwaysIncludeServerUrl="false" 
     encodeNames="true" 
     languageEmbedding="always" 
     languageLocation="filePath" 
     lowercaseUrls="false" 
     shortenUrls="true" 
     useDisplayName="false" />

Notice the +

1
  • 1
    The + was crucial and it fixed it. Thank you Mark
    – Qwerty
    Mar 13 '17 at 14:22
1

It looks like you have patched it correctly.

It is most likely one of these reasons:

  • The project was renamed and the assembly still has the old name and therefore the reference is wrong
  • You have supplied an incorrect namespace + class in the configuration (you examples looks correct though)
  • The dll is not published together with the project.
  • The dll is an old version which does not contain all code

First you need to double check that MySite.Foundation.Common.dll is present in the website bin folder. If the dll is present and is named MySite.Foundation.Common.dll, then 2 of the options are exhausted and then must be either be a namespace + class mismatch or old dll. You can use JetBrains DotPeek (for free) to reflect the dll to make sure it contains the needed code.

0

Two possibilities:

  1. Your assembly name is incorrect. As per your configuration, make sure it should be MySite.Foundation.Common.dll.
  2. Confirm the assembly contains your classes using a Reflector.
3
  • Should I include the extension .dll in the config ?
    – Qwerty
    Mar 13 '17 at 11:52
  • No, .dll is not required there in config. It should contain only the name of DLL file. Mar 13 '17 at 11:53
  • That's what I did. I do not have reflector now. But I add other classes, variables or methods in a class of this project and build & publish. It works fine when referenced in other projects.
    – Qwerty
    Mar 13 '17 at 11:53

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.