0

i am trying to update a listitem in a event receiver on FeatureActivated in a document library.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb myweb = site.OpenWeb())
                {


                SPList list = myweb.Lists.TryGetList("Document Library");
                SPListItemCollection items = list.GetItems();

                for (int i = 0; i < items.Count; i++)
                {

                    SPListItem item = items[i];



                    Guid fieldID = item.Fields.GetFieldByInternalName("Age").Id;

                    string valueField = item[fieldID].ToString();
                    valueField = "";
                    item.Update();
                }
                }
            }
        });            
    }

My field does not get empty?

  • can you use a debugger? – Gwny Jun 16 '15 at 8:44
  • No because when activating my debugger and activating the feature it does not go in to my feature i just standards still – Batistuta Jun 16 '15 at 8:56
  • my list is in a teamsite... – Batistuta Jun 16 '15 at 9:32
2

Because you do not Change the value in the field, you just Change the value in your variable.

you have to

item[fieldID] = "";
  • is still does not clear all the items? – Batistuta Jun 16 '15 at 7:03
  • Is your field a textfield or a number-field? – Leopold Lerch Jun 16 '15 at 7:34
  • besides: If you are using SPContext.Current.Site the code will still run under the Current-Users Privileges. If you want to run it with elevated privileges you have to create a new Context using SPSite site = new SPSite(<URL or id>) – Leopold Lerch Jun 16 '15 at 7:35
  • I have already tried with that, its HTML for publishing – Batistuta Jun 16 '15 at 7:38
2

Avoid using SPContext inside receiver. SPContext doesn't exist in a feature receiver. Instead use the Properties object as it includes a reference to the site or web

use following

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web = properties.Feature.Parent as SPWeb;
        using (SPSite site = new SPSite(web.Site.ID))
        {
            using (SPWeb rootWeb = site.OpenWeb(site.RootWeb.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    //Your code
                });
            }
        }
    }
0

Try

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = SPContext.Current.Site)
        {
            using (SPWeb myweb = site.OpenWeb())
            {


            SPList list = myweb.Lists.TryGetList("Document Library");
            SPListItemCollection items = list.GetItems();

            for (int i = 0; i < items.Count; i++)
            {

                SPListItem item = items[i];
                item["Age"] = string.Empty
                item.Update();
            }
            }
        }
    });            
}
  • Still the same.. – Batistuta Jun 16 '15 at 7:22
  • Is it throwing any error? – users1100 Jun 16 '15 at 7:25
  • No i dont get any errors – Batistuta Jun 16 '15 at 7:27
  • it is not good to make a using with spcontext.current.site. you don't need to dispose it, sharepoint will do it for you. this can even cause problems later. – Gwny Jun 16 '15 at 8:43

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.