Question

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

Was it helpful?

Solution

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

OTHER TIPS

Deploy your feature on SharePoint Central Administration web application.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top