Getting Your Feet Wet Writing Code For SharePoint – Part 5 of 5

Originally posted on: http://geekswithblogs.net/SoYouKnow/archive/2009/09/02/getting-your-feet-wet-writing-code-for-sharepoint-ndash-part-once-more.aspx

Here we are at the end of the series.  Han finds out that Luke and Leia are siblings, Soylent Green is people, Snape was really a good guy, Mikey forgot that he put some jewels in his marble bag and thus saved the Goon Docks, and Indiana Jones was named after his dog. 

When we last left off, our courageous developer had written all of the code in a console applications to handle the following requirements:

Now we come to the end where the remaining requirement is:

  • Document needs to be executed on a scheduled basis

So, in this last entry we will clean up our program to make it more parameter driven and I will walk you through setting up a scheduled task on your server.  Unfortunately, there is no new SharePoint related development in this entry.  If you are already comfortable with command line parameters and creating a scheduled task, then you may not get much out of this (although I appreciate you stopping by).

For those of you who decided to stick around, let’s quickly summarize what we are going to do with the code from the previous entry:

  • Create Command Line Parameters to handle List Name, View GUID, and DocLib name.
  • Do some housekeeping and clean up the code a little bit.
  • Set up application as a Scheduled Task on our SharePoint server.

Very easy, nothing to ‘wow’ you with here, but there’s a first time to do everything.  If this is yours I want to make it as painless as possible.

Create Command Line Parameters to handle List Name, View GUID, and DocLib name.

Well, obviously we don’t want our application to only work for one list, one view, and one document library.  Also, we don’t have a large number of variables to really it make it worth the time and effort to have some sort of config file.  What we do have is the ability to use command line parameters.   Back in the old days, before fancy color screens, when memory was measured in kilobytes, back when you actually had to know what a push and pop was you had to execute every application at the command prompt.  Ahhh.. my C64… I still remember the first time I typed in ‘Load “FileName”,8,1’  

Anyway, if it’s not broke, don’t fix it.  Command line parameters work great for our requirements here as well especially since we are using a console application.  Do I need to go into more detail about this?  If you think that ‘args’ in “public static void Main(string[] args)” has something to do with pirates then maybe you need a quick tutorial:

Command Line Parameters Tutorial

So, what we are going to do in our code is first make sure that the user entered all the necessary parameters, if not we’ll exit the application with a message on the screen indicating the needed arguments:

static void Main(string[] args)
{
    //make sure that user enters 3 and only 3 parameters
    if (args.Length != 3)
    {
        Console.WriteLine("Please enter all three parameters. 1. List Name 2. View Guid 3. Document Library to save export to");
        return;
    }
.
.
.
}

If you were really on top of things, you’d validate that the parameters entered were the right ones instead of letting the application bomb. You could at least validate that it is a properly formatted GUID fairly easily.

Anyway, now we need to store the parameters in our local variables:

static void Main(string[] args)
{
    //make sure that user enters 3 and only 3 parameters
    if (args.Length != 3)
    {
        Console.WriteLine("Please enter all three parameters. 1. List Name 2. View Guid 3. Document Library to save export to");
        return;
    }
    
    // 1st parameter is the name of the sharePoint list to export
    String listName = args[0].ToString(); 
    
    // 2nd parameter is the GUID for the specific view of the list 
    String sGUID = args[1].ToString();  

    // 3 parameter is the name of the document library to upload exported list to
    String docLibName = args[2].ToString(); 
.
.
.
}

And that’s all there is to it really.  We now get our values from the command line parameters.  So, when our application is executed from the command prompt it looks something like:

SPListExportToExcel.exe AuthorizedRequestors 7674ABF8-C71D-49FB-A9A3-7FD45993EDEC Approval%20Documents

Keep in mind that the parameters are separated by spaces, so if your list name or document library name has a space in it, you need to replace the space with “%20”.

Do some housekeeping and clean up the code a little bit.

So, before you compile and install this bad boy on your Production SharePoint Server you should do some code clean-up.  I won’t bore you with them here, but if you have any questions on how to do them please let me know.  However, some suggestions are:

  • Remove “Console.ReadLine();” from the application so that it doesn’t wait for an enter key press.
  • Check to make sure Excel spreadsheet does not exist before creating it (delete it if it does)
  • Catch all errors and format them in a readable way so that someone running the application manually can make sense of it.
  • Write errors out somewhere so that someone knows about them (maybe a log file?) or have the application send an email to someone if there is a problem.
  • Combine the methods to find a SharePoint List and Document Library (repeating code is bad)
  • Make sure you can upload large files to document library (remember I’m reading entire file into memory before uploading)
  • Test… Test… Test…

I could go on and on.. but you get the picture.  As is, this is not a production ready application IMO.  So please don’t stick it in production.  If you ignore that warning, then don’t come pointing any fingers at me. 

Set up application as a Scheduled Task on our SharePoint server

So, either you have made the changes above to make this a production worthy application or you are just playing around and want use this as a Scheduled Task.  Exactly how do you do that? It’s actually pretty simple.  Just follow these steps after you’ve installed the application.

1.  Under Control Panel double click “Scheduled Tasks”

image

2.  Double click on “Add Scheduled Task”

image

3.  This opens up the “Scheduled Task Wizard”, Click Next

image

4.  Click on Browse, and browse to your application

image 

5.  Select how often you want to execute this application, and guess what? yep. click Next

image

6.  Enter more specific information if required, click Next

image

7.  This is a very important step.  Enter the credentials that the application will run under.  These credentials must have access to the List and Document Library in question (click Next)

image

8.  This will take you to a final screen, make sure you check the “Open advanced…” check box, then click “Finish”

image

9.  The Properties for you Scheduled Task will now appear.

image

10.  In the “Run” Text box is where we add our Command Line Parameters.  So append to the file name the parameters you need.  For the screen shot above you would end up with this in the Run textbox (based upon our previous code):

C:\tools\SPListExportToExcel\SPListExportToExcel.exe AuthorizedRequestors 7674ABF8-C71D-49FB-A9A3-7FD45993EDEC Approval%20Documents

11.  Click OK.  You can now, right click on your scheduled task and select “Run” to manually run your scheduled task and make sure it all works correctly

image

And, there you have it.  Really easy stuff. And this brings us to the end of the series, kind of ended with more of a whimper than a bang don’t you think?  Well, I do hope you learned something from all this!  I’m still contemplating what my next series should be.  If you have any ideas or feedback I’d love to hear it.  Thanks again for sticking around for the whole thing and please feel free to contact me if you have any questions or suggestions.

Families in Germany who are facing divers health problem, such persons can buy drugs from the Web without prescription. With the market flooded with divers web-sites selling sundry medicaments, purchasing medicines from th WEB is no longer a trouble for common man. Certain medications are used to treat infections caused by dental abscesses. Of course it isn’t all. If you’re concerned about erectile soundness problem, you probably know about Xenical and Hoodia. Probably either adult knows about Garcinia. (Read more PhentermineXenical). The symptoms of sexual health problems in men turn on impossibility to have an hard-on sufficient for sexual functioning. Certain medications may add to sex drive difficulties, so its vital to cooperate with your health care professional so that the prescription can be tailored to your needs. Preparatory to grab Levitra or other preparation, speak to your pharmacist if you have any allergies. Talk to your soundness care producer for more details. Preparatory to ordering this remedy, tell your doctor if you are allergic to anything.