1

I am trying to read the value within my CustomApplicationPage.aspx.cs file and update the value from No to Yes.By default when this site column was created in the Content Type Hub's custom content type, its value is set as No.In my code am doing some manipulation and have to set this value to Yes.But unfortunately I am not able to get the value of this Yes/No column-checkbox and update it.Here is my code.

      item = list.Items.GetItemById(Convert.ToInt32(stritemid));
       LogFile("after calling getitembyid in page load"," getitembyid 
       stack trace in item==","page load_aftr_getting item"+ item.Title);
        spWebobj.AllowUnsafeUpdates = true;
          documentName = item.Name;
           documentUrl = item.Url;
            if (item != null && item.Fields["CMPDocument"] != null)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPFieldBoolean blnCMPDocumentField = 
                  item.Fields["CMPDocument"] as SPFieldBoolean;
                 bool cmpDocumentValue =                               
           (bool)blnCMPDocumentField.GetFieldValue(item["CMPDocument"]
             .ToString());
                        item["CMPDocument"] = true;
                        item.Update();
                        item.ParentList.Update();
                    });
                }
                catch (Exception)
                {
                    PortalLog.LogString("Error in Updating the 
             CMPDocument column in current item  " + item.Name + " and 
                its URL " + item.Url.ToString() + ");
                }

Want to know whether the code item["cmpdocument"] =true is the correct way of updating a yes/no field.

2

Try,

item = list.Items.GetItemById(Convert.ToInt32(stritemid));      
try
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using(SPSite site = new SPSite(SPContext.Current.Web.Url))
        {
            using(SPWeb web = site.OpenWeb())
            {
                SPListItem elevatedItem = item.ParentList.GetItemById(item.ID);
                if(elevatedItem.File.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    elevatedItem.File.CheckOut();
                }
                else
                {
                    elevatedItem.File.CheckIn(string.Empty);
                    elevatedItem.File.CheckOut();
                }
                item["CMPDocument"] = "1";
                item.Update();
            }
        }       
    });
}
catch (Exception)
{
    PortalLog.LogString("Error in Updating the 
CMPDocument column in current item  " + item.Name + " and 
its URL " + item.Url.ToString() + ");
}

You don't need to update the list for updating an item. And also you should open the SPSite and SPWeb objects under SPSecurity.RunWithElevatedPrivileges and fetch SPListItem if you want to update by the System Account in code snippet.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.