1

I have written a cusom timer job where in while Activating the feature i am using the below code. It works in devlopment but when i deployed it in QA i get the below error. The user is a Farm administrator. Is any thing wrong in my code?

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {


                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPSite objsite = SPContext.Current.Site;
                    SPWeb objweb = SPContext.Current.Web;
                    SPSite ElevatedSite = new SPSite(objsite.Url);

                    SPWeb ElevatedWeb = ElevatedSite.OpenWeb(objweb.Url);


                    objweb.AllowUnsafeUpdates = true;
                    SPWebApplication webApp = objweb.Site.WebApplication;
                    foreach (SPJobDefinition job in webApp.JobDefinitions)
                        if (job.Name == "TaskAlertTimerJob") job.Delete();
                    string key = "mySiteUrl";
                    string value = objweb.Url;
                    TaskAlert tmrJob = new TaskAlert(webApp);
                    bool isKeyExists = tmrJob.Properties.ContainsKey(key);
                    if (isKeyExists)
                    {
                        tmrJob.Properties.Remove(key);
                    }
                    tmrJob.Properties.Add(key, value);
                    SPMinuteSchedule schedule = new SPMinuteSchedule();
                    schedule.BeginSecond = 0; //to start immediately
                    schedule.EndSecond = 59; //use this if timer job is to end after some seconde
                    schedule.Interval = 1; //number of minutes
                    tmrJob.Schedule = schedule;
                    tmrJob.Update();

                    objweb.AllowUnsafeUpdates = false;





                });
            }
            catch (Exception ex)
            {
                //log exception if any
            }
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                //remove the scheduled job
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPSite objsite = SPContext.Current.Site;
                    SPWeb objweb = SPContext.Current.Web;
                    SPSite ElevatedSite = new SPSite(objsite.Url);

                    SPWeb ElevatedWeb = ElevatedSite.OpenWeb(objweb.Url);

                    objweb.AllowUnsafeUpdates = true;
                    SPWebApplication webApp = objweb.Site.WebApplication;
                    foreach (SPJobDefinition job in webApp.JobDefinitions)
                        if (job.Name == "TaskAlertTimerJob") job.Delete();
                    objweb.AllowUnsafeUpdates = false;




                });
            }
            catch (Exception ex)
            {
                //log exception if any
            }
        }

in Execute Method I have written like this

public override void Execute(Guid targetInstanceId)
        {

               if (!string.IsNullOrEmpty(this.Properties["mySiteUrl"].ToString()))
            {
                mySiteUrl = this.Properties["mySiteUrl"].ToString();
            }

            if (!string.IsNullOrEmpty(mySiteUrl))
            {
                using (SPSite mySite = new SPSite(mySiteUrl))
                {
                    using (mySiteWeb = mySite.OpenWeb())
                    {

                        DateTime dtNow = DateTime.Now;
                        SPList Sourcelist = mySiteWeb.Lists.TryGetList("TaskList");
                        SPQuery mySourceListQuery = new SPQuery();
                        mySourceListQuery.Query = "Query"
                        SPListItemCollection mySourceItemColl = Sourcelist.GetItems(mySourceListQuery);
                        if (mySourceItemColl.Count > 0)
                        {


                            int count = 0;
                            foreach (SPListItem mySourceListItem in mySourceItemColl)
                            {
                                       // My Code Goes Here                          


                                count++;
                            }//End of for loop



                        } //End of if loop



                    }
                }
            }



        }

My Constructor and method is like this

public TaskAlert(SPWebApplication webApplication):base("TaskAlertTimerJob", webApplication, null, SPJobLockType.None)
        {
            Title = "TaskAlertTimerJob";
        }

ERROR: The SPPersistedObject, TaskAlert Name=TaskAlertTimerJob, could not be updated because the current user is not a Farm Administrator. 45db89e8-dbef-4c1c-8405-46acb8ab6e82 06/18/2013 06:34:57.33 w3wp.exe (0x2108) 0x1F0C SharePoint Foundation General 75f8 Medium Feature Activation: Feature 'TaskAlertSchedular_TaskAlertFeature' (ID: 'b434bb08-caea-44c3-b8d6-cc3a32f3507c') was activated at URL http://santest.com/sites/MySite. 45db89e8-dbef-4c1c-8405-46acb8ab6e82

2 Answers 2

0

Simple,

Your using runwithelevated privlages means your account that your running under is application pool not the current logged in user!

Instead you need to also use impersonation with elevated priv to get the wanted result!

check my answer here in detail! :)

SharePoint -access to path is denied

6
  • That could work, or the dangerous step of elevating the app pool account to Farm Administrator. This violates a million best practices, but depending on the application this may be the way to go.
    – tekiegreg
    Jun 18, 2013 at 18:52
  • iv only stated runwithelevated priv along with impersonation becuause he was using it ;) in the first place! the fact that its a timer job doesnt matter! also the fact that your ment to create a new spweb/site object and only do what you need to under that to do what you want to do and than run under normal account! just like anything you do! so this is not going against any best practices as you sometimes need to do this and is actualy a best practice! as stated in my answer, the elevated priv is not run as he needs farm account ;) inorder to run under dii account he needs that :)
    – Ali Jafer
    Jun 19, 2013 at 7:57
  • ALI You are Awsome :-) It works with your code the link which you have send. You are great and thanks for your immediate help. Can i use the code which you poast in my blog with your permission please Jun 19, 2013 at 8:59
  • yup thats no probs! go for it! glad to be of some help!
    – Ali Jafer
    Jun 19, 2013 at 9:49
  • Good point, RunWithElevatedPrivileges on a Timer Job means he's now the Config DB/Farm Account. That's an account that absolutely should not be Farm Administrator. Your idea is just Fine Ali, but I was just trying to make a point of something he could do, but shouldn't...
    – tekiegreg
    Jun 19, 2013 at 13:55
-1

Deploy your feature on SharePoint Central Administration web application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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