0

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)
          {
          base.ItemAdded(properties);
          string currURL = properties.Web.Url; //            
                ListItem.ParentList.ParentWebUrl.ToString();
          SPUserToken sysToken = null;
        //get the system account security token
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite current = new SPSite(properties.SiteId))
            { sysToken = current.SystemAccount.UserToken; }
         });

        //SPListItem _currentItem = properties.ListItem;
        //_currentItem["Title"] = DateTime.Now.ToString();
        //_currentItem.Update();
        string fullurl = properties.Web.Url + "/" + 
            properties.ListItem.Url;
        this.EventFiringEnabled = false;
        SPListItem item = 
          properties.List.GetItemById(properties.ListItemId);
        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**
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
               using (SPSite site = new SPSite(properties.SiteId, 
             sysToken))
                {
                    //We're effectively logged in as system account while 
                 //running this code 
                    using (SPWeb web = 
                 site.OpenWeb(properties.ListItem.Web.ID))
                    {
                        //get a reference to our list
                        SPList myList = web.Lists[properties.ListId];
                        //get a reference to our new item 
                        //SPListItem mitem = 
                   myList.Items.GetItemById(properties.ListItemId);
                        SPListItem mitem = 
                   myList.Items.GetItemById(properties.ListItemId);
                        bool allowUnsafe = web.AllowUnsafeUpdates;
                        try
                        {
                            web.AllowUnsafeUpdates = true;
                    //break inheritance if it hasn't been done already
                            if (!mitem.HasUniqueRoleAssignments)
                            {
                                mitem.BreakRoleInheritance(false);
                                web.AllowUnsafeUpdates = true;
                            }
                           for (int counterr = 
             mitem.RoleAssignments.Count - 1; counterr >= 0; counterr--
             )
                            {
                                mitem.RoleAssignments.Remove(counterr);
                            }

Console application to register the event receiver is given below:

         try
           {
            using (SPSite site = new SPSite(myurlll))
            {
                using (SPWeb siteWeb = site.OpenWeb())
                {
        SPDocumentLibrary list = siteWeb.Lists["Process Data Repository"] 
           as SPDocumentLibrary;
                    SPEventReceiverDefinition defItemAdding = 
            list.EventReceivers.Add();
         defItemAdding.Assembly = "DocApprovalEvtRecvrProject, 
                Version=1.0.0.0, Culture=Neutral, 
             PublicKeyToken=6f69de9316923162";
              defItemAdding.Class = 
              "DocApprovalEvtRecvrProject.DocApprovalEvtRecvr";
                    defItemAdding.Name = "ItemAdded Event";
                    defItemAdding.Type = SPEventReceiverType.ItemAdded;
                    defItemAdding.SequenceNumber = 1000;
                    defItemAdding.Synchronization = 
                SPEventReceiverSynchronization.Synchronous;
                    defItemAdding.Update();
                }
              }
          }
          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.