Timer Jobs to Copy and Delete Files in a Farm

March 1 2010 29 comments

There is sometimes a need to copy files in a SharePoint farm on different locations, than what can be specified in wsp-packages. Usually the best way to do it farm-wide, is to use Features and timer jobs. Timer jobs can be specified so that they are executed on every single machine in a SharePoint farm. In SharePoint 2007 the most common need we have had, is the copying of .resx and .browser files to a web application’s App_GlobalResources and App_Browsers directories. For most of you this is fairly basic information, but there might be readers and new comers to whom this might be interesting.

On web application level Feature’s feature activation we use the generic copy timer task as the following:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var webApp = properties.Feature.Parent as SPWebApplication;
    if (webApp == null) return;
    var src = Path.Combine(properties.Definition.RootDirectory, "x.browser");
   
    foreach (var settings in webApp.IisSettings)
    {
        var dst = Path.Combine(Path.Combine(
        settings.Value.Path.FullName, "App_Browsers"), "x.browser");
       
        var touch = Path.Combine(Path.Combine(
            settings.Value.Path.FullName, "App_Browsers"), "compat.browser");
       
        var job = new CopyTimerJob(
            "Sininen Meteoriitti X Control Adapter Copy Timer Job",
            webApp, null, SPJobLockType.None)
        {
            Schedule = new SPOneTimeSchedule(DateTime.Now)
        };
        job.Properties.Add("src", src);
        job.Properties.Add("dst", dst);
        job.Properties.Add("touch", touch);
        job.Update();
    }
}

The code that does the actual copying of the file is defined in generic copy timer job class:

public class CopyTimerJob : SPJobDefinition
{
    // Constructors stripped off to save some space
   
    public override void Execute(Guid targetInstanceId)
    {
        try
        {
            var src = Properties["src"] as string;
            var dst = Properties["dst"] as string;
            var touch = Properties["touch"] as string;
            if (src == null || dst == null) return;
            var directoryName = Path.GetDirectoryName(dst);
            if (!Directory.Exists(directoryName)) return;
            File.Copy(src, dst, true);
            if (touch != null && File.Exists(touch))
            {
                File.SetLastWriteTime(touch, DateTime.Now);
            }
        }
        finally
        {
            Delete();
        }
    }
}

For deletes (on Feature deactivation) we have a similar timer job:

public override void Execute(Guid targetInstanceId)
{
    try
    {
        var src = Properties["src"] as string;
        var touch = Properties["touch"] as string;
        if (src == null) return;
        if (File.Exists(src)) File.Delete(src);
        if (touch != null && File.Exists(touch))
        {
            File.SetLastWriteTime(touch, DateTime.Now);
        }
    }
    finally
    {
        Delete();
    }
}

Popularity: 6% [?]

29 comments to “Timer Jobs to Copy and Delete Files in a Farm”

  1. [...] Timer Jobs to Copy and Delete Files in a Farm (SharePoint Blues)There is sometimes a need to copy files in a SharePoint farm on different locations, than what can be specified in wsp-packages. Usually the best way to do it farm-wide, is to use Features and timer jobs. Timer jobs can be specified so that they are executed on every single machine in a SharePoint farm. In SharePoint 2007 the most common need we have had, is the copying of .resx and .browser files to a web application’s App_GlobalResources and App_Browsers directories. For most of you this is fairly basic information, but there might be readers and new comers to whom this might be interesting. [...]

  2. [...] job to copy the meteoriitti_error.browser in web application’s App_Browsers directory (see timer jobs). The timer job is used to handle farm deployment scenario where there are multiple web front ends. [...]

  3. Michael says:

    i am using http://sptimerjobadmin.codeplex.com/ and all I want to do is get my custom properties to show up in the config page.

    What is the best way to create dynamic properties for SPTimerJobs?

    regards,

    Michael

  4. clear says:

    My brother recommended I might like this blog. He was entirely right.

    This post truly made my day. You can not imagine simply how much time
    I had spent for this info! Thanks!

  5. This site was… how do I say it? Relevant!! Finally I have found something that helped me.

    Many thanks!

  6. Great write-up, I am normal visitor of one¡¦s web site, maintain up the nice operate, and It’s going to be a regular visitor for a lengthy time.

  7. webpage says:

    Нeya i’m for the primary time here. I founԀ this board аndd
    I tto find It really useful & it helρed me out a lot.
    I hope to offer onee thingg back anԀ help others such aѕ you aided mе.

    My web blog … webpage

  8. I аll theе tme emailed this websitе post page to all mʏ friendѕ,
    because if like to read it then my links will too.

    My website – wyndham rewards

  9. here says:

    Hello to every one, it’s genuinely a nice for me
    to visit this web site, it contains important Information.

  10. Kemudian sehabis akunnya telah dibuat, seterusnya adalah lakuin deposit.
    It appears that the airplane did not suffer an abrupt
    structural failure or catastrophic bomb attack that caused an immediate crash, but no one can be certain what caused the
    airplane to fly under control for hundreds of miles before it presumably plunged into
    the sea. Beberapa hal yang wajib anda pahami ialah total taruhan yang anda bet, dan perkiraan soccer yang
    anda punya.

  11. Excellent goods from you, man. I’ve understand your
    stuff previous to aand you’re just too wonderful.
    I actually like what you have acquired here, certainly
    like whyat you’re statinjg and the wway in which you say
    it. You make it enjoyable and you still take care of to keep it wise.
    I can’t wait to read far more from you. This is really a terrific website.

  12. Very nice post. I just stumbled upon your weblog and wanted to say that
    I have truly loved browsing your blog posts. In any case I will be subscribing in your feed and I hope you write once more very soon!

  13. el noubuck says:

    Hi to every body, it’s my first pay a quick visit of this web site;
    this weblog carries amazing and really fine material
    in support of visitors.

  14. My relatives every time say that I am wasting my time here
    at net, except I know I am getting know-how every day by reading
    thes pleasant posts.

    Feel freee too surf to my blog :: club penguin free membership

  15. porn says:

    Younger Busty Perky Ebony Uncovered Sexy young ebony teen gf blowjob Perfect ebony novice gf exposed Younger Ebony Homemade Sextape Scorching Gentle Skinned Ebony Fucked Perfect Light Skinned Ebony Anal videos or photos are pretty delicate
    area of interest. Plenty of folks like to watch
    it, few of them are literally doing it. And we like to look at teen anal videos,
    so check out our web page with plenty of ass pounding clips and pics.
    coming back for extra, and with our regular members area updates and free Chubby ebony
    lesbians licking each other If that’s the case she might be
    unfit for the onerous tasks she must meet ifthe struggle for black freedom is to deliver us
    out of our historical bondageinto a truly new and liberated condition. Real
    Ghetto Emotive Ebony Gf Scorching Slutty Ebony Gf Cumshoted Ebony
    Brilliants

  16. Elijah says:

    This website wɑs… how ɗo I ѕay it? Relevant!! Finally I’ve fօund ѕomething that helped
    me. Тhank you!

    Alsߋ visit mү homepage – buy instagram followers – Elijah -

  17. Cornelius says:

    Excellent blog post. I definitely appreciate this site.
    Keep writing!

  18. Georgetta says:

    It’s going to be end of mine day, but before finish I am reading this impressive paragraph
    to increase my know-how.

  19. Boris says:

    Hi there to all, the contents existing at this website
    are in fact remarkable for people experience, well,
    keep up the nice work fellows.

  20. Seznamka vám ve dne і v noci nabízí na naší platformě možnost zapojit
    se Ԁo erotickéɦо chatu s registrovanými členy, nebo ѕi rovnou domluvit schůzku.
    Promente svoje nejtajnejší erotické fantazie а
    sny vve skutecnost s nekým neokoukaným а neznámým a odvažte se.
    Vyzkoušejte tߋ sami na vlastní kůži a užijte ѕi sex
    v Plzni.

  21. You will get the tաenty siх Jaam tracks tɦat teach simple chord progressions ɑnd the computeг software tɦat will assist you
    tune уօur guitar.

    Feel free tߋ surf tο mʏ web blog – best way to learn guitar scales free

  22. Ellie says:

    With Media Challenge Productions all their clients
    get benefit from a toolkit of skills, experience and technology.
    If you use corporate videos or promotional videos as the tool
    then, this would enhance your business and would give you a competitive edge.
    It is important to let potential users what the benefits of
    using these products are.

  23. klima tamiri says:

    I do not even know how I finished up right here, however I believed this post was
    good. I do not recognize who you might be however definitely you are going to a well-known blogger when you are
    not already. Cheers!

  24. Mormon says:

    ‘ That movie is about the way we handle stress in our society and that’s another important topic.
    - Put one in your bedroom and watch TV with Bluetooth Headphones
    while your significant other sleeps peacefully, no fighting.
    The film industry has seen big changes from digital distribution, marketing and even fundraising.

  25. This is because the bad credit loan company’s promotions such loans will not verify the borrower’s
    credit rating. Eliminating all of the red-tape is a method for
    lenders to produce their customers ignorant to the fact that they’re
    putting themselves in a poor spot, from a financial viewpoint.

  26. There are numerous benefits to bad credit lenders that you probably aren’t alert to basically.

  27. These kinds of Search marketing boxes normally realistic, healthy and balanced as a result receive just about every customer service necessary for some product. Link Building Services

  28. Bho says:

    Usually granted for a brief periods of time, they
    allow you to catch-up on past due obligations until the next paycheck
    is due. sales@auric79.com

Leave a Reply