0

Very Very new to sharepoint, much more to customizing it.

However, new job, as part of my task in the morning, I got to create 2 items every day. These have all the same values, except the date that changes in the item title.

What I am looking is to have either a button in the ribbon, or a link under the new item button.

this button/link would create these 2 items, with proper properties selected, and custom title.

The more automated, the better.

Sorry, I have been Google and cant seem to find it.

  • Do you have any limitations? No Server Side code, for example? – wjervis Jun 10 '14 at 18:21
  • This sounds like a candidate for a scheduled site workflow via SPD. Can you use SharePoint Designer? – Eric Alexander Jun 10 '14 at 18:33
  • @wjervis: yup, no server side code would be an example. I can always do a request for it...but. – user3202532 Jun 11 '14 at 0:40
  • @PirateEric : Not even certain we have the Sharepoint Designer on our workstation – user3202532 Jun 11 '14 at 0:43
  • My answer will not work then, unless you feel like making that request. What is your access level on the site? Are you an admin? Power User? You can install SharePoint Designer on your computer, but what you'll be able to do depends on what your permissions are. If you are able to, the workflow PirateEric mentioned is your best bet. – wjervis Jun 11 '14 at 11:02
2

A timer job would work great for your situation.

Timer jobs are background processes that are run by SharePoint. As the name implies, these can be ran on a schedule, such as every day.

This link explains how to create a timer job with detail. For convenience, here is the code. I also provide some basic instruction (link has screen shots if needed). I also made minor edits to the code, as the example uses a task list, and a 15 minute schedule.

To create a timer job, create a SharePoint project in Visual Studio, and create a farm solution (must be farm, sandboxed solutions cannot deploy timer jobs!).

Add a class to the project, and add the following to it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Data;
using System.IO;

namespace TimerJobApplication
{
    public class CustomTimerJob : SPJobDefinition
    {
        public CustomTimerJob() : base() { }
        public CustomTimerJob(string jobName, SPService service) : base(jobName, service, null, SPJobLockType.None)
        {
            this.Title = "Timer Job Title";
        }
        public CustomTimerJob(string jobName, SPWebApplication webapp) : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Timer Job Title";
        }
        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApp = this.Parent as SPWebApplication;
            SPList list = webApp.Sites[0].RootWeb.Lists["List Name"];
            SPListItem newItem = list.Items.Add();
            // Set metadata for the item here.  For example, to set the title:
            newItem["Title"] = "New Item" + DateTime.Now.ToString();
            newItem.Update();
        }
    }
}

Add a WebApplication scoped feature to your project. Add a feature receiver to the feature. The code for the feature receiver should be as follows:

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace TimerJobApplication.Features.CustomTimerJobFeature
{
    [Guid("e6ea0027-1187-419d-b357-306244d0ae37")]
    public class CustomTimerJobFeatureEventReceiver : SPFeatureReceiverimer
    {
        const string JobName = "Timer Job Title";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
                    SPSite site = properties.Feature.Parent as SPSite;
                    DeleteExistingJob(JobName, parentWebApp);
                    CreateJob(parentWebApp); 
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private bool CreateJob(SPWebApplication site)
        {
            bool jobCreated = false;
            try
            {
                CustomTimerJob job = new CustomTimerJob(JobName, site);
                SPDailySchedule schedule = new SPDailySchedule();
                // Change to hour that you wish this to run.  Currently Set to 730AM
                schedule.BeginHour= 7;
                schedule.BeginMinute= 30;
                job.Schedule = schedule;

                job.Update();                
            }
            catch (Exception)
            {
                return jobCreated;
            }
            return jobCreated;
        }
        public bool DeleteExistingJob(string jobName, SPWebApplication site)
        {
            bool jobDeleted = false;
            try
            {
                foreach (SPJobDefinition job in site.JobDefinitions)
                {
                    if (job.Name == jobName)
                    {
                        job.Delete();
                        jobDeleted = true;
                    }
                }
            }
            catch (Exception)
            {
                return jobDeleted;
            }
            return jobDeleted;
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            lock (this)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
                        DeleteExistingTimerJobFromSite(this.JobName, parentWebApp);
                    });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
}

Then debug/deploy.

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.