How to access fields of List Item in ItemAdding and ItemUpdating event handlers into SharePoint

June 23, 2009

Welcome Reader,

I have moved to a new blog site, so you can get same content from below new site URL!!

http://dishasharepointworld.blogspot.com/2009/06/how-to-access-fields-of-list-item-in_22.html

Please feel free to post your comments/feedback into new blog, I’ll be more than happy to assistant you.

Sorry for any inconvenience!!

Disha Shah

About these ads
  1. Alexandra
    July 28, 2009 at 7:17 am

    Hi,
    Great post. I have tried it but don’t seem to get it working. I implemented the ItemAdding event handler but instead of sharepoint displaying the error nicely I get a server error page, although the error message I wanted to display appears. Is there another place I should modify? Or am I missing something? Any help would be nice.
    Thank you.

  2. Alexandra
    July 28, 2009 at 7:52 am

    Hi,
    Solved the problem. It had something to do with my web.config . The changes I made:



    Thanks.

    • dishashah08
      July 28, 2009 at 1:26 pm

      Alexandra

      Thanks for comment and also giving feedback. What changes you did to web.config to solve your problem? I think you made changes for CallStack and CustomErrors. If possible please share details.

      Thanks and Regards

  3. ken
    July 28, 2009 at 5:16 pm

    Hi Disha,

    This is exactly what I’m looking for! However, I have a quick question. In your foreach loop, why did you use the type DictionaryEntry? Where did this value come from, and how should I change it?

    Thanks for the post

    • dishashah08
      July 28, 2009 at 5:44 pm

      Hi

      Thanks for appreciation and comments. Now when you save a particular ListItem, all field values that we entered of that listitem store as type DictionaryEntry (System.Collections).

      I think, you can also try below lines of code and may be it solves your problem.

      public override void ItemAdding(SPItemEventProperties properties)
      {
      string Name = “”;
      using (SPWeb web = properties.OpenWeb())
      {
      Name = web.Lists[properties.ListId].Fields[“Name”].InternalName;

      }
      properties.AfterProperties[Name] = “ChangedValue”;
      }

      Enjoy !!!!!!!

  4. Harish
    August 23, 2009 at 10:34 pm

    Hi Disha,
    I wrote similar code to validate the data while user is entering in the columns of a list but writing code in event handlers with properties.errormessage= some text will display a new page with go back to the site option where user has to go back and reenter the data from scratch, instead we can use jscript so that the error message will be shown in a pop-up window and data in other columns remains unaltered and after pressing ok they can edit the column with correct data.I am thinking so but I have no idea how to do that.Can you give me some idea?

  5. Harish
    August 23, 2009 at 10:51 pm

    Hi Disha,
    I am sorry to include this comment here but I want your suggestion. I have 2 lists namely ‘Tasks’ list and ‘stages’ list.Tasks list will have an ‘TID'(task id)’Create a Stage?’ and ‘link to the Stage list’ columns along with some other columns, so when creating a item(new task) in tasks list if they say ‘yes’ for ‘Create a stage?’ column they will see a link to that corresponding stage list in the ‘link to stage list’ column and this data will get copied in to that Stages list automatically all this i did using event handlers.So the stages list will consist of all the stages for all the tasks in the tasks list with unique task ID’s and Stage ID’s.Now where I got struck is I have to show them only stages of a that particular task in the Stages list if already existing or I have to copy this data and make them create new stage when they click on ‘link to stage list’ in tasks list, it means filtering has to be done automatically in the stages list with respect to the TID value in the Tasks list.Except filtering part I wrote all other code for item added/updated and deleted cases.Do you have any idea or webpart apart from writing custom filter code?
    Any suggestion is appreciated.

    • dishashah08
      August 24, 2009 at 2:31 pm

      Harish,

      I like your requirement and it’s very interesting. Good work!

      Now if you are looking for any Filter web part, then I think you can use and play with out of the box Filters web part, “SharePoint List Filter” web part is one of the out of the box web part, in this web part you need to select List Name, select filter column names and then you can send output or results to any another web part into page …

      So please see whether you can leverage and use “SharePoint List Filter” web part or any other out of the box Filters web part or not. I think custom coded filter web part is last option if you didn’t find anything from out of the box.

      Good Luck!!

      Disha Shah :)

  6. Harish
    August 24, 2009 at 4:12 pm

    Thanks for your suggestion Disha. I will look in to Sharepoint list filter webpart and let you know what solution I choose finally.

  7. Harish
    September 2, 2009 at 3:21 am

    Hi Disha,
    I have tried using list filter webpart and query string url filter webpart but I didnt get the solution may be I didnot make a wright approach using these filters but I just modified my list view filter like filter when column ID is equal to[ID] and in my event handlers in the column where I hardcoded the link to another list I did smthng like after http://….TIDView.aspx? + “?ID=” + newitem[“ID”]); . In fact this is the basic idea of a query string filter.
    Thus I got the soulution.

    • dishashah08
      September 2, 2009 at 2:36 pm

      Hey Harish

      That’s really good that you got your solution by filter web part. Great Job!!

      Disha Shah :)

  8. Harish
    September 2, 2009 at 3:40 am

    Hi,
    This is Harish again.I got an issue with event handlers I have a list in a publishing site on which I wrote 2 event receivers and each of which furthur includes 3 types of event handlers(item added/deleting/updating same in both the cases). Here the problem is in item updating case I am getting Save conflict error I thing every developer using updating event handlers might have seen this . I am even using disable and enable event firings but still unable to get rid of that error. I am using updating event because while item updating for a column ‘Status’ I need to check what its value is before.properties and after.properties(open/cloesd). Can you help me?

    • dishashah08
      September 2, 2009 at 2:46 pm

      Hi Harish,

      Yeah I also got same type of problem while working with event handlers, below are the steps which I followed to resolve my issue, you can also try to do same thing and see whether it�s will solve your problem or not.

      For every update to a list item the SPList.update() method needs to be called to save the changes back to database. The pattern which I like to follow is to retrieve the SPWeb GUID from the current site, then call a separate instance of SPWeb object per individual list update. I know that this is a tedious pattern and doesn’t necessarily have to be used on all occasions, but may be it will save you from this annoying little exception.

      Sample Code:

      protected void itemUpdate(string FieldName, string FieldValue, SPSite site, Guid webID, Guid listID, Guid itemID)
      {
      try
      {
      using (SPWeb web = site.OpenWeb(webID))
      {
      web.AllowUnsafeUpdates = true;
      SPList list = web.Lists[listID];
      SPListItem item = list.Items[itemID];
      item[FieldName] = FieldValue;
      item.Update();
      list.Update();
      web.AllowUnsafeUpdates = false;
      }
      }
      catch(Exception ex)
      {
      Response.Write(“An error occured while updating the items”);
      }
      }

      When disposing of SPWeb object, remember CONTEXT things also. If you retrieve SPWeb object from the current context (SPContext.Current.Web), you cannot and should not dispose of SPWeb object.

      If you dispose of the Web which is being used by the current context, it’s the equivalent of ripping the ground from under your feet!!

      If you are doing multiple list updates the best approach would be to open and dispose of a separate instance of SPWeb object.

      Disha Shah :)

  9. Harish
    September 17, 2009 at 9:25 pm

    Thanks for the Code Disha,
    I have already tried this code but this didnot work but finally I solved the problem by replacing properties.listitem.update() with properties.listitem.SystemUpdate(false)in item updating event handler.

    Regards,
    Harish

    • dishashah08
      September 18, 2009 at 12:24 am

      Hi Harish,

      It’s good to know that you got the solution.

      Thanks for sharing the information.

      -Disha Shah

  10. Sai
    March 8, 2010 at 1:47 pm

    Hi Disha

    I need to create alert through sharepoint object modal for logged in users. If logged in user subscibe alert in my custom webpart for any docuemnt libraray item, than when ever that subscribed item will modify, user will get alert with that document url. i have a webpart which shows the selected documents from document library. Over there i need to give this fcility.

    Thanks in advance.

    Regards
    Sai

    • dishashah08
      March 12, 2010 at 2:22 am

      Hi Sai

      I think you can fulfill your requirement by Alerts Facility from SharePoint Interface only, there is no need to go for SharePoint Object Model.

      Please let me know why you want to use SharePoint object model?

      Best Regards
      Disha Shah

  11. shiva
    May 5, 2010 at 12:55 pm

    Thanks for sharing ur knowledge to others..

  12. Anisia
    May 6, 2010 at 12:17 pm

    Hi,
    Great work. I am trying to used it in my sharepoint site but the entry.Key doesn’t seem to show the internal name. I have a content type that I have created programatically with columns that I gave internal names, but they don’t seem to show in entry.Key. Can you please help me?

    Thanks.

    Have a great day,
    Anisia

    • dishashah08
      May 7, 2010 at 12:46 am

      Hi Anisia

      Entry.key may return the display name of the field. So you can get internal name by using this code

      SPList list = properties.OpenWeb().Lists[properties.ListId];

      foreach (DictionaryEntry entry in properties.AfterProperties)
      {

      Strint strIntername = list.Fields[entry.Key].Intername;

      }

      Hope this helps!!!Please let me know and describe in detail if I am unable to understand your problem correctly.

      Thanks & Regards
      Disha Shah

  13. June 3, 2010 at 9:40 am

    Thanx but i want to access item field insade property such as Due Time and Assigned to
    how can i do that ??

    • dishashah08
      June 3, 2010 at 12:30 pm

      Hi Tarhoni,

      “Due Time” and “Assigned To” these two are Fields for the current List Item? If yes, then you can add like these

      string strDueDate = string.Empty;
      string strAssignedto = string.Empty;

      foreach (DictionaryEntry entry in properties.AfterProperties)
      {
      if (entry.Key.Equals(“DueDate”)) \\internal name of duedate
      {
      strDueDate = entry.Value.ToString();
      }
      else if (entry.Key.Equals(“AssignedTo”)) \\internal name of AssignedTo
      {
      strAssignedto = entry.Value.ToString();
      }

      }

      Hope this helps!!!

      Thanks & Regards
      Disha Shah

  14. hardik Shah
    August 5, 2010 at 7:14 pm

    Hi Disha,

    Nice blog.
    Disha, i m getting problem in ItemAdded event where i m not able to display error message.

    Properties.Errormessage = “error”;
    properties.cancel = true;

    Thanks & Rgds,
    Hardik Shah

    • dishashah08
      August 6, 2010 at 2:53 am

      Hi Hardik

      Thanks for the appreciation!!!

      One question raised in my mind for your question,

      If it does not display that error message it means there is some problem before that line.If possible, Can you please post that code before that line so it makes easier for me to solve your problem?

      Thanks & Regards
      Disha Shah

  15. hardik shah
    August 6, 2010 at 7:45 am

    Hi Disha,

    Thanks you so much.
    since Last 2 days i have been trying to get item value in the variable in itemadding event. but each time it gives error null ref.
    But using dictionaryEntry i can able to get the values…. :)

    Regarding my earlier post:
    ItemAdded problem:
    I have written spquery and its give me the record. till that everything is good.

    now;
    SPListItemColletion TimeList = CRBlist.GetItems (TimeQuery);

    if(TimeList.Count > 1)
    {
    Properties.ErrorMessage = “Error massage”;
    Properties.Status = SPEventReceiverStatus.CancelWithError;
    Properties.Cancel = true;
    }
    }
    }

    This code in not in any try or catch block. This code is in ItemAdded event.

    • dishashah08
      August 6, 2010 at 12:40 pm

      Hardik

      Just change the order of the statement that you have written for eventhandler.

      //Properties.Status = SPEventReceiverStatus.CancelWithError; –Do not write this one and try
      properties.Cancel = true;
      properties.ErrorMessage = “Deleting is not supported.”;

      Hope this helps!!!
      Disha Shah

  16. sapna deshmukh
    September 15, 2010 at 1:04 pm

    HiDisha,

    How we can debug sharepoint features?

    Thanks
    Sapna

  1. No trackbacks yet.
Comments are closed.
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: