Monday, August 27, 2012

Delete/Purge Files in Document Library with Workflow Using Specifc Duration


Delete/Purge Files in Document Library with Workflow Using Specific Duration

This is a Out-of-Box workflow in MOSS, to automatically delete files from a document library 120 days after they were uploaded. The doc library was being used strictly as a transfer point and needed to be kept clean.

Please fallow the below steps to create a WF.

1. Start a new workflow using SharePoint Designer by going to File -> New -> Workflow.

2. Give the Workflow a name and select the target doc library or list.

3. Set the start event. I set mine to automatically start when the item is created or changed. Click Next.

4. Enter a name for Step 1 such as `Pause for 120 days'

5. Do not select a condition because we want it applied to all items.

6. Under Actions select `Pause for duration'.

7. Change to 120 days, 0 hours, and 0 minutes.

8. On the right choose to `Add workflow step'

9. Once again, give the step a name and no condition.

10. Select Delete Item under Actions and set it to Current Item.

11. Click Finish to save the workflow.



Friday, August 24, 2012

Programmatically creating Wiki Pages


The Windows SharePoint Services Object Model provides some methods for Wiki Pages Library Programmability. Using these methods, we can create new Pages in Wiki Pages Library and set a default page in the Wiki Pages Library. The following snippet is the sample for creating Wiki Pages in a Wiki Pages Library.
using (SPSite site = new SPSite("http://sharepoint"))
{
 SPWeb currentWeb= site.RootWeb;
 SPList spList = currentWeb.Lists["Site Pages"];
  SPFolder wikiRootFolder = spList.RootFolder;
 if (spList != null) {
 try {
  SPFile wikiPage = wikiRootFolder.Files.Add(String.Format("{0}/{1}", wikiRootFolder, "WikiPage.aspx"),                                           SPTemplateFileType.WikiPage);
   SPListItem wikiItem = wikiPage.Item;
   wikiItem[SPBuiltInFieldId.WikiField] = "Wiki Page Content "; 
 wikiItem.UpdateOverwriteVersion();
 wikiRootFolder.Update();
 }
}
}
For getting Wiki Page information, there are some suggestions from Waldek Mastykarz. I would Thank him for his great post. Please refer to the following link: http://blog.mastykarz.nl/programmatically-creating-wiki-pages/

Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object

Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object. This is a common Error that every SharePoint developer will do.

We cannot use SPContext.Current.Web in feature receivers. They can be run by the timer service or powershell which does not have access to the SPContext.

So We can create instances for out SPsite and SPWeb as mentioned below.

using (SPSite site = (SPSite)properties.Feature.Parent)
{
   using (SPWeb web = site.OpenWeb())
   {
            Our code
            ---------------------
            ---------------------
---------------------
                       
   }
}

Friday, August 3, 2012

Fiscal Year Calculation in SQL Server

Fiscal Year Calculation in SQL Server

DECLARE @SuppliedDate DATETIME,
@StartMonth INT,
@SessYr NVARCHAR(7)
Set @SuppliedDate='2012-01-01'
Set @StartMonth=10

 SELECT SessYr= CASE WHEN DATEPART(MONTH,@SuppliedDate)>@StartMonth
 THEN CAST(DATEPART(YEAR,@SuppliedDate) AS VARCHAR(4))+'-'+RIGHT(CAST(DATEPART(YEAR,@SuppliedDate)+1 AS VARCHAR(4)),2)
 ELSE CAST(DATEPART(YEAR,@SuppliedDate)-1 AS VARCHAR(4))+'-'+RIGHT(CAST(DATEPART(YEAR,@SuppliedDate) AS VARCHAR(4)),2) END

OR

DECLARE @BusinessDate datetime
Set  @BusinessDate='2012-10-01'

SELECT MONTH(@BusinessDate)as BMonth,YEAR(@BusinessDate) as BYear,FiscalYear = CASE
WHEN MONTH(@BusinessDate) < 10 THEN YEAR(@BusinessDate)-1
ELSE YEAR(@BusinessDate) END