2
votes

I have this event receiver c# class that I am trying to implement on a Sharepoint site. It did not work. I have deployed it from visual studio 2010 after it was build ok. Does anyone see what is the problem? Is the code ok? or is the problem on the SP? Thank you. - here is the new code

    using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverCFolder.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {

            try
            {
                if (properties.ListTitle == "CF") // list where the item was added 
                { // if item was added to this list then create a folder on -  Dlib - list
                    UpdateFolder(properties);
                }
            }
            catch (Exception ex)
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = ex.Message;
                properties.Cancel = true;
            }
        }

        private void UpdateFolder(SPItemEventProperties properties)
        {
            string foldername = properties.ListItem["Title"].ToString();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //inside RunWithElevatedPriviliges I need to open a new site (an elevated site) 
                    using (SPSite site = new SPSite(properties.Web.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SPList list = web.Lists.TryGetList("DLib"); // this is doc Library where the new folder will be created
                            //note that we are creating a list item, not a folder - a folder IS a list item 
                            SPListItem createdFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);
                            if (createdFolder != null)
                            {
                            createdFolder["Name"] = foldername;
                                createdFolder.Update();
                            }
                            list.Update();
                        }
                    }
                });
            }
            finally { }
        }
    }
}
3

3 Answers

2
votes

Don't do this: SPUser privilegedAccount = properties.Web.AllUsers[@"SHAREPOINT\SYSTEM"]; Read up on using SPSecurity.RunWithElevatedPrivileges. See the MSDN documentation here.

Also don't do a using (SPSite... and inside the using block you try to get the web via SPContext.Current - that web won't be elevated anymore.

The correct way is something along these lines (I didn't try this, so it' just to give you an idea where you are headed):

private void UpdateFolder(SPItemEventProperties properties)
{
    string foldername = properties.ListItem["Title"].ToString();

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
            //inside RunWithElevatedPriviliges I need to open a new site (an elevated site)
        using (SPSite site = new SPSite(properties.Web.Site.ID))
        {
            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists.TryGetList("ListTitle"); //is that really the list title?
                //note that we are creating a list item, not a folder - a folder IS a list item
                SSPListItem createdFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);
                if (newFolder != null)
                {
                    createdFolder["Name"] = foldername;
                    createdFolder.Update();
                }
                list.Update();
            }
        }
    });
}

Also try to debug your code, set breakpoints etc.

1
votes

I had to get folder name like this:

string foldername = Convert.ToString(properties.AfterProperties["Title"]);
0
votes

Did you try to debug it? try to debug and tell us what error you are getting. BUT before you debug first use sharepoint manager to see if your event receiver is attached properly.

If you dont know how to debug sharepoint event receiver then please see this