Search code examples
c#sharepointsharepoint-2010

Sharepoint 2010 Programmatically Update List Item in Event Handler


Background
We have a custom developed installed .WSP on a SharePoint 2007 environment and have been in the process of upgrading to 2010. With the upgrade the custom event trigger no longer worked so trying to update and make it work in 2010. But I am running into one issue. Original developers no longer here and I've been the lucky one to have to figure this one out without much of a background with SP Dev.

Goal
When a new list item is created trigger event. Within event, create a shared folder using Item Name and return url, create a wiki-page using item name and include shared document link and return url to wiki page. Part three is update newly created list item with the New Folder url and Wiki Page URL.

Issue
I've gotten the first two parts working but so far have been unable to update the newly created list item with the new Links. I'm able to get the links. I've tried all the basic stuff for updating the list that I have been able to find online with no luck. Nothing to complicated(or so I think). But code is included below. VS is not installed on the server so unable to run debug mode, I don't have direct access to the server. When you create the item there are no client/user side error. Haven't been able to find a log file that has any, that is if it collects errors if the script were to fail out.

Initiation of the Event

public class CreateWikiAndFolder : Microsoft.SharePoint.SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        try
        {
            //this.DisableEventFiring();
            base.EventFiringEnabled = false;
            string sUrlOfWikiPage = string.Empty;
            string sUrlOfNewFolder = string.Empty;
            string sSubsiteRUL = string.Empty;
            string sCurrentItemTitle = properties.ListItem["Title"].ToString();
            string sWikiListName = "TR Wikis";
            string sDocLibName = "Shared Documents";
            string sTRListID = "TR Status";

            if (sTRListID.ToUpper().Equals(properties.ListTitle.ToString().ToUpper()))
            {
                //Create the Folder
                sUrlOfNewFolder = CreateFolder(properties.ListItem.Web, sDocLibName, sCurrentItemTitle);

                //Create the Wiki
                string ItemDispFormUrl = String.Concat(properties.ListItem.Web.Url, "/", properties.ListItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, "?ID=", properties.ListItem.ID.ToString());
                sUrlOfWikiPage = CreateWiki(properties.ListItem.Web, sWikiListName, sCurrentItemTitle, ItemDispFormUrl, sUrlOfNewFolder);


                //Update the current TR Item
                //Have tried. properties.ListItem["WikiURL"] = sUrlOfWikiPage + ", " + "Wiki";
                SPListItem myListItem = properties.ListItem;
                SPFieldUrlValue shareFolderURLValue = new SPFieldUrlValue();
                shareFolderURLValue.Description = "Shared Folder";
                shareFolderURLValue.Url = sUrlOfNewFolder ;
                myListItem["SharedFolder"] = shareFolderURLValue;

                //I've tried each one separate and together to no luck
                myListItem.UpdateOverwriteVersion();
                myListItem.Update();
                //properties.ListItem.UpdateOverwriteVersion();
            }
            base.EventFiringEnabled = true;  
        }
    }
}

Note that this is the last thing needed to be figured out for our upgrade.


Solution

  • Got it working. I did both of these at the same time so I'm not sure if it was the combination of both or only one of the items. But one I removed the myListItem.UpdateOverwriteVersion(); line and surrounded the item updated with web.AllowUnsafeUpdates being set to true before and then back to false afterwards.

    Also as a note to others, you need to save the properties.ListItem to its own SPListItem which you then update versus trying to manipulate the values at the properties.ListItem["Attribute"], and then update the properties.ListItem.Update. SharePoint doesn't allow the latter option so you have to save to an independent SPListItem, and then modify and update that one. This might not be the best SharePoint lingo, but that is what needs to be done.