1

I need to change the displayname of a list using "ItemUpdating". The source of the new name is the "Title"-field of the item which fires the event. Is this possible? I tried it with:

if ((properties.ListTitle == "Fahrzeuge") || (properties.ListTitle == "Räume"))
            {
                base.ItemUpdating(properties);

                try
                {
                    base.DisableEventFiring();

                    string origCalTitle = properties.ListItem["Title"].ToString();
                    string newCalTitle = properties.AfterProperties["Title"].ToString();

                    if (origCalTitle != newCalTitle)
                    {
                        using (SPWeb web = properties.OpenWeb())
                        {
                            CultureInfo origCulture = Thread.CurrentThread.CurrentUICulture;

                            web.AllowUnsafeUpdates = true;

                            SPList changeList = web.Lists[origCalTitle];

                            Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);

                            changeList.Title = newCalTitle;
                            changeList.Update();

                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }
                finally
                {
                    base.EnableEventFiring();
                }
            }

I also tried it on some other way, like ItemUpdating and Afterproperties but nothing happens. Unfortunately I am not a pro in programming Event Receiver und also Google doesn´t help me out, so I need help.

Thanks for all effort!

2 Answers 2

1
public override void ItemUpdating(SPItemEventProperties properties)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            if ((properties.ListTitle == "Fahrzeuge") || (properties.ListTitle == "Räume"))
            {

            string currentTitle = properties.ListItem["Title"].ToString();
            string newTitle = properties.AfterProperties["Title"].ToString();

            if (currentTitle != newTitle)
            {
                properties.AfterProperties["Title"] = calTitle;
            }

        }
    });
}
0

Add the code in the ItemUpdated.

I have gone through your code, sorry i missed that your elevating user lol, i can see that your using the properties object. As your using that its only read only. You have two things you can do here:

1) create the spsite and spweb object to update the list

or

2) use the itembefore and after to change the title

ItemUpdating not saving field values

    string calTitle = properties.ListItem["Title"].ToString();

    using (SPSite site = new SPSite(properties.WebUrl))
    using (SpWeb web = site.OpenWeb())
    {
       web.AllowUnsafeUpdates = true;

       SPList changelist = web.Lists[calTitle];

       changelist.Title = calTitle;
       changelist.Description = calTitle;

       changelist.Update();
       web.AllowUnsafeUpdates = false;
    }

or

if i got this right the properties should look somthing like this:

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            if ((properties.ListTitle == "Fahrzeuge") || (properties.ListTitle == "Räume"))
            {

                string currentTitle = properties.ListItem["Title"].ToString();
                string newTitle = properties.AfterProperties["Title"].ToString();

                if (currentTitle != newTitle)
                {
                    properties.AfterProperties["Title"] = calTitle;
                }

            }
        });
    }

https://stackoverflow.com/questions/14994981/how-to-update-one-list-from-another-using-itemupdated-event-receiver-in-sharepoi

http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122

14
  • Sorry, but both ways wont work for me :-/ Mar 21, 2013 at 10:09
  • just ammended my answer by using changelist.SystemUpdate(false);
    – Ali Jafer
    Mar 21, 2013 at 10:34
  • I updated my first post with alle my code for ItemUpdated. Isn´t it the same way as you suggest except of cahangelist.SystemUpdate(false)? Mar 21, 2013 at 13:04
  • One more thing... I´m doing this for SP2007. Because of this I use base.DisableEventFiring(); Mar 21, 2013 at 13:14
  • are you getting any errors in eventviewer? does the account have privlages to make changes
    – Ali Jafer
    Mar 21, 2013 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.