Showing posts with label feature event receiver. Show all posts
Showing posts with label feature event receiver. Show all posts

Thursday, July 4, 2013

Implementing a simple custom Timer Job in SharePoint 2010

Simple implementation of an SPTimerJob which moves all files in a specified folder to another, prefixing the filenames with a timestamp. The interval is set to every minute.

1. Create a new empty SharePoint 2010 project
2. Create the following class (make sure the folders specified have been created:


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

namespace HotFolderJob
{
    public class FileMoverJob : SPJobDefinition
    {
        #region Constructors

        //Constructor
        public FileMoverJob() : base() { }

        //Constructor
        public FileMoverJob(string name, SPWebApplication webApp) :
            base(name, webApp, null, SPJobLockType.ContentDatabase) { this.Title = "Hotfolder Timer Job"; }

        #endregion Constructors

        #region Methods - Overridden

        public override void Execute(Guid targetInstanceId)
        {
            const string sourcePath = @"C:\TimerJob\DropFolder";
            const string targetPath = @"C:\TimerJob\TargetFolder";

            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] fullPathFiles = System.IO.Directory.GetFiles(sourcePath);

                foreach (string fullPathfile in fullPathFiles)
                {
                    var originalFileName = Path.GetFileName(fullPathfile);   
                    var destFile = Path.Combine(targetPath, (string.Format("{0:yyMMdd_Hmmss}_", DateTime.Now) + originalFileName)); 
                    File.Move(fullPathfile, destFile);
                    //File.Copy(fullPathfile, destFile);
                }
            }
            
        }

        #endregion Methods - Overridden
    }
}
3. Add a feature, name it as you please
4. Add an event receiver to it and define the activated/deactivated methods as follows:


using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace HotFolderJob.Features.HotfolderFeature
{
    [Guid("a2bb921d-dda5-4f0c-923c-da8004a4b09a")]
    public class HotfolderFeatureEventReceiver : SPFeatureReceiver
    {
        const string JOB_NAME = "HotFolderJob";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME)
                    job.Delete();
            }

            FileMoverJob fileMoverJob = new FileMoverJob(JOB_NAME, site.WebApplication);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            fileMoverJob.Schedule = schedule;
            fileMoverJob.Update();

        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME)
                    job.Delete();
            }

        }

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
5. Set the feature scope to Site (applies for this example)
6. Deploy and see the magic happen



Wednesday, June 20, 2012

Securing SharePoint lists by code

Rather than stuffing configuration keys into *.CONFIG files, it's more convenient to use SharePoint lists instead. This makes it easier to alter configuration settings, as for each change we would need to deploy a new .CONFIG file and involve SysAdmins, get approval from the management, et cetera.

But obviously, you do not want all users to have access to these, if you will, configuration lists. Manually setting all permissions per list and user group would be a nag. Therefore, I'm doing this by code, using the feature event receiver. I basically fetch the SPLists required, break the inheritance and re-set the permissions. The 'userGroup' variable holds the name of the user group associated with the feature. Same goes for the 'prefixList' variable.



public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            const string userGroup = "TeamGary";
            const string prefixList = "config.";

            using (SPSite site = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    List<string> fdtListTitles = new List<string>();

                    // get all feature-related SPList titles
                    foreach (SPList list in web.Lists)
                    {
                        if (list.Title.Substring(0, 7) == prefixList)
                        {
                            web.AllowUnsafeUpdates = true;

                            // break inheritance from parent
                            list.BreakRoleInheritance(false);

                            // remove all permissions from list, except for userGroup
                            SPGroupCollection groupCollection = list.ParentWeb.SiteGroups;
                            SPGroup group = groupCollection[userGroup];
                            SPRoleDefinitionCollection roleDefCollection = list.ParentWeb.RoleDefinitions;
                            SPRoleDefinition roleDefinition = roleDefCollection["Read"]; // set access level here
                            SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);

                            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

                            list.RoleAssignments.Add(roleAssignment);

                            list.Update();
                        }
                    }
                }
            }
        }