am using itemadded event receiver to updatea field when end user uplaods a document in the document library. i have associated content types as well with the doc.lib.Now when i am trying to access the value of field which is part of the content type while debugging, am getting the null value.

      public override void ItemAdded(SPItemEventProperties properties)
          string currURL = properties.Web.Url; //            
          SPUserToken sysToken = null;
        //get the system account security token
            using (SPSite current = new SPSite(properties.SiteId))
            { sysToken = current.SystemAccount.UserToken; }

        //SPListItem _currentItem = properties.ListItem;
        //_currentItem["Title"] = DateTime.Now.ToString();
        string fullurl = properties.Web.Url + "/" + 
        this.EventFiringEnabled = false;
        SPListItem item = 
        item["Title"] = "VGaPPROVAL DOCS_" + DateTime.Now.ToString();

        //Pending With Stage1
        //Stage1 Approved
        //Stage1 Rejected
        //Pending With Stage2
        //Stage2 Approved
        //Stage2 Rejected
        if (item["VGWorkflow Status"].ToString().Equals("Pending With 
          Stage1")) // **am getting error here as NULL**
               using (SPSite site = new SPSite(properties.SiteId, 
                    //We're effectively logged in as system account while 
                 //running this code 
                    using (SPWeb web = 
                        //get a reference to our list
                        SPList myList = web.Lists[properties.ListId];
                        //get a reference to our new item 
                        //SPListItem mitem = 
                        SPListItem mitem = 
                        bool allowUnsafe = web.AllowUnsafeUpdates;
                            web.AllowUnsafeUpdates = true;
                    //break inheritance if it hasn't been done already
                            if (!mitem.HasUniqueRoleAssignments)
                                web.AllowUnsafeUpdates = true;
                           for (int counterr = 
             mitem.RoleAssignments.Count - 1; counterr >= 0; counterr--

Console application to register the event receiver is given below:

            using (SPSite site = new SPSite(myurlll))
                using (SPWeb siteWeb = site.OpenWeb())
        SPDocumentLibrary list = siteWeb.Lists["Process Data Repository"] 
           as SPDocumentLibrary;
                    SPEventReceiverDefinition defItemAdding = 
         defItemAdding.Assembly = "DocApprovalEvtRecvrProject, 
                Version=, Culture=Neutral, 
              defItemAdding.Class = 
                    defItemAdding.Name = "ItemAdded Event";
                    defItemAdding.Type = SPEventReceiverType.ItemAdded;
                    defItemAdding.SequenceNumber = 1000;
                    defItemAdding.Synchronization = 
          catch (Exception ecpool)
        {        }

    The above code throws error in .update()
  • You are definitely using the internal name of VGWorkflow Status column? Is the column a required field or not? – Paul Strupeikis Mar 15 '16 at 9:43
  • No,am not using the internal name. am using display name.Do you mean to say I have to use internal name? – userAZLogicApps Mar 15 '16 at 9:48
  • 1
    Then stop using that and start using internal names and see if it fixes the issue. The other thing - if the column can have an empty value use: Convert.ToString(item["VGWorkflowStatus"]).Equals("Pending With Stage1"), as this won't give an exception if there is no value assigned, whereas your code will. – Paul Strupeikis Mar 15 '16 at 9:51
  • Update: i have content types associated with. now am uploading a document, by default am getting the document and before selecting the associated content type and update the fields within that content type , the document is uploaded.i think, that's the reason it failed. so my question is how to stop triggering this workflow till i fill the metadata of the associated content type. – userAZLogicApps Mar 15 '16 at 10:06
  • 1
    want to know why my question was is been given -1 vote. i have explained my issue and with my tried code. – userAZLogicApps Mar 25 '16 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.