Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John 3 posts 73 karma points
    15 days ago
    John
    0

    Umbraco : How to set culture and hostnames of node programmatically .

    Hello guys ,

    I'm so younger user of Umbraco Cms . I would be glad if some of you help me to solve my problem .

    I have Json file , it includes all information about my content tree. After upload the file to Web API , application create content tree programmatically.

    Problems begin later:

    After creating any node , I would like to set the culture and hostnames programmatically .

    If some of you hit on with this case , please ask me to set it.

    Thank you very much ,

    Best wishes!\ enter image description here

  • Kevin Jump 1247 posts 7610 karma points MVP 2x c-trib
    15 days ago
    Kevin Jump
    0

    Hi

    You can set the Domain and Hostname using the DomainService

    the main thing you need to do once you have a reference to the service, is to create a UmbracoDomain object and then set the domain, languageId and the Id to the content you want it to apply to (e.g your site homepage).

    you need to get the language Id from the LocalizationService (see bottom)

    // assuming you have worked out the languageId and
    // contentNode is the node you want to set this on.
    
    var  domain = new UmbracoDomain(domainName);
    domain.LanguageId = languageId;
    domain.RootContentId = contentNode.Id;            
    domainService.Save(domain);
    

    the languageId is the Id of the installed language on umbraco (so as they appear in the Developer/Languages - normally this will just be the order they are in (so top one is 1) but if you have added & removed languages that might not be true - so to be certain use the LocalizationServices

    you can do this by culture code or Iso Language Code

    // by name 
    var lang = _localizationService.GetLanguageByCultureCode(fName.Value);
    
    // by iso code
    var lang = _localizationService.GetLanguageByIsoCode(culture.Value);
    

    with this you can get the id lang.Id for the first snippet of code.

    Kevin

  • John 3 posts 73 karma points
    15 days ago
    John
    0

    Oh thank you , bro!

    Look at this :

                var contentService = ApplicationContext.Current.Services.ContentService;
                var publishDate = DateTime.Now;
    
                var content = contentService.CreateContent(name, parentPageId, docTypeAlias);
                content.ReleaseDate = publishDate;
                content.CreateDate = publishDate;
                content.UpdateDate = publishDate;
    
                content.SetValue("propertyName", someData);
                var result =contentService .SaveAndPublishWithStatus(content);
    
                return result.Success;
    

    Can you impove this method with you top description? Add set of culture.

  • Kevin Jump 1247 posts 7610 karma points MVP 2x c-trib
    15 days ago
    Kevin Jump
    2

    Hi I would have a sperate thing for the domain language, you only need set it on at one level and all the content pages below will inherit it.

  • John 3 posts 73 karma points
    15 days ago
    John
    0

    I have to ask different culture for different nodes.

  • Kevin Jump 1247 posts 7610 karma points MVP 2x c-trib
    14 days ago
    Kevin Jump
    0

    Hi John,

    Yeah unless every node is different you will probably only need to set the domain and language a few times, at the highest level for each part of the content tree.

    i am not sure but i think setting the domain and language on every single node will result in quite a lot of extra data and config being stored and might impact on the performance of your site.

Please Sign in or register to post replies

Write your reply to:

Draft