2

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 { }
        }
    }
}
FAA
  • 499
  • 1
  • 7
  • 16

3 Answers3

2

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.

Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • I have updated the code. can somebody please look at it. it seems I am missing simple thing. the code works if I use just one list to create defined folder name in the code. but it won't work with 2 lists. any ideas. thank you – FAA Sep 21 '12 at 23:01
  • Where exactly does your exception come, and what is the message of the exception. If you are able to debug, you can give us more information. – Dennis G Sep 23 '12 at 12:59
  • here is the line that has the problem: -------------------- stringfoldername =properties.ListItem["Title"].ToString(); ------------------------------ Object reference not set to an instance of an object. also where are you refering to by newfolder: -----------------if (newFolder != null)---------------- ------thank you – FAA Sep 24 '12 at 20:15
1

I had to get folder name like this:

string foldername = Convert.ToString(properties.AfterProperties["Title"]);
FAA
  • 499
  • 1
  • 7
  • 16
0

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

Raheel
  • 595
  • 8
  • 21
  • It debugs fine. and I know it works in sharepoint. see my other comments. can you look to the updated code. best regards. – FAA Sep 21 '12 at 23:02