none
Unable to update welcome page of subsite RRS feed

  • Question

  • I am unable to change the welcome page of subsite. I have written an event receiver to capture item add event on which the url of of welcome page needs to be updated. Below mentioned is the code for the same. Please let me know where I am missing anything

     public override void ItemAdded(SPItemEventProperties properties)
            {

                try
                {
                    // taking the current SPWeb context running under the anonymous account
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(properties.WebUrl))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                SPFieldUrlValue url;
                                TaxonomySession taxSession = new TaxonomySession(web.Site);
                                TermStore termStore = taxSession.TermStores[Constants.TermStore];
                                Group storeGroup = termStore.Groups[Constants.Group];
                                TermSet termSet = storeGroup.TermSets[Constants.NLTermSet];
                                web.AllowUnsafeUpdates = true;
                                if (properties.List.ToString() == "NewsLetter")
                                {
                                    SPListItem item = properties.ListItem;
                                    string title = item["Title"].ToString();
                                    string option = item["Active"].ToString();
                                    termSet.IsOpenForTermCreation = true;
                                    termStore.CommitAll();
                                    termSet.CreateTerm(title, CultureInfo.CurrentCulture.LCID);
                                    termStore.CommitAll();
                                    SPFolder rootFolder = web.RootFolder;
                                    if (option == "True")
                                    {
                                        url = new SPFieldUrlValue(item["HomeURL"].ToString());
                                        string strUrl = url.Description.ToString();
                                        rootFolder.WelcomePage = strUrl.Substring(1, strUrl.Length - 1);
                                        rootFolder.Update();

                                    }
                                }
                                web.AllowUnsafeUpdates = false;
                                termSet.IsOpenForTermCreation = false;
                                termStore.CommitAll();
                                web.Dispose();
                            }

                        }
                    });
                }
                catch (Exception ex)
                {
                    DAL.ULSLogger.LogErrorInULS("EventReceiver:NewsLetter; Function:ItemAdded; Exception:" + ex.Message, TraceSeverity.Medium);
                }
            }

    Its very urgent, please respond back with your suggestions asap

     
    Wednesday, July 11, 2012 9:38 PM

Answers

All replies

  • Just a couple of things first:

    If you want to update something on the site, shouldn't you then use the ItemUpdated event instead?

    Have you verified that the WelcomePage property is set to a valid url that you can eg. test in a browser ?  

    And have you also verified that the "option" field is set to True, since that's the one that in your code decides if the url is updated or not?


    Kind Regards Bjoern
    Blog

    Wednesday, July 11, 2012 10:41 PM
    Moderator
  • I have 2 events itemadded and itemupdated where I am updating the welcome page url.

    url that I am passing is valid and it has been tested also by manually updating the welcome page url

    Option is true then only i am able to set it. control is going through that if condition

      if (option == "True")
                                    {
                                        url = new SPFieldUrlValue(item["HomeURL"].ToString());
                                        string strUrl = url.Description.ToString();
                                        rootFolder.WelcomePage = strUrl.Substring(1, strUrl.Length - 1);
                                        rootFolder.Update();

                                    }

    but once it executes this line

     rootFolder.Update();

    the welcome page url sets to default welcome page of subsite

    Wednesday, July 11, 2012 11:12 PM
  • have you enabled publishing ?

    If so, you can use PublishingWeb.DefaultPage Property



    Mano Mangaldas | Blog : www.howtosp.com 

    • Marked as answer by ManishJha Friday, July 13, 2012 12:33 AM
    Wednesday, July 11, 2012 11:24 PM
  • thanks Mano. Issue resolved
    Friday, July 13, 2012 12:33 AM