Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint TimerJob: Using SPListItem.Update() in an instance of a class / static method

I have created a timer job (SPJobDefintion) which is properly registered and working in a sharepoint 2010 environment.

In the Execute method of the Timer Job I'm able to use the following code, which sets a field of a specific SPListItem. This works fine.

using (SPSite mySite = new SPSite(mySiteId))
{
  using (SPWeb myWeb = mySite.OpenWeb(myWebId)))
  {
    SPList myList= myWeb.Lists[myListId];
    SPListItem myItem = myList.GetItemById(myItemId);

    myItem["myField"] = myValue;
    myItem.Update();
   }
}

For design reasons I would like to put that code in a separate method of a separate class. So I created the following class:

namespace myNamespace
{
  class myClass
  {

    [...some stuff...]        

    public myClass()
    {
      [...more stuff...]
    }

    public void setField()
    {
      using (SPSite mySite = new SPSite(mySiteId))
      {
        using (SPWeb myWeb = mySite.OpenWeb(myWebId)))
        {
          SPList myList = myWeb.Lists[myListId];
          SPListItem myItem = myList.GetItemById(myItemId);

          myItem["myField"] = myValue;
          myItem.Update();
        }
      }
    }
  }
}

Actually I would expect that calling this following code in the Execute method of the timer job should end in the same result.

MyClass myClassInstance = new myClass();
myClassInstance.setField();

Unfortunately I'll get the following exception:

Microsoft.SharePoint.SPException was unhandled by user code
  Message=""
  Source=Microsoft.SharePoint
  ErrorCode=-2147418113
  NativeErrorMessage=FAILED hr detected (hr = 0x8000ffff)

  NativeStackTrace=""
  StackTrace:
       bei Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
       bei Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, [...])
       bei Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, [...])
       bei Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, [...])
       bei Microsoft.SharePoint.SPListItem.Update()
       bei myNamespace.myClass.<>c__DisplayClass1.<setField>b__0()
   InnerException: System.Runtime.InteropServices.COMException
       Message=<nativehr>0x8000ffff</nativehr><nativestack></nativestack>
       Source=""
       ErrorCode=-2147418113
       StackTrace:
            bei Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrUpdateItem(String bstrUrl, [...])
            bei Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, [...])
       InnerException: 

When I put the setField() method directly in the class where the Execute method is, the method will work properly. If I declare this method as static, the same Exception will occur.

I also tried to run the SPListItem.Update(), where the exception occurs, with elevated privileges (should be not the reason, because the code should be run in the OWSTIMER context). I also tried to play around with the allowUnsafeUpdates() on the SPSite and the SPWeb. Both with no success.

In the web I found only one description of this problem http://codekicker.de/fragen/sharepoint-2010-Sharepoint-2010-TimerJob-wirft-Fehler-timer (in German), unfortunately without any solution.

What else could be the reason, that this constellation will not work? Any idea?

like image 755
Mat S Avatar asked Nov 03 '22 10:11

Mat S


1 Answers

It definitely needs myWeb.AllowUnsafeUpdates = true; before updating any list item. do not forget to reset it back to false after updating it.

One thing I would make sure is to make sure the user the code is running as have full rights to update the item. I would suggest, logging in as the site administrator and try to run the code from a button click event (rather than a timer job) or some other event just to make sure that it is not a user permission issue.

Thanks

Senthil S

like image 54
Senthil S Avatar answered Nov 08 '22 06:11

Senthil S