Sunday, February 28, 2010

Another reusable component - Site Columns and Content Type generator

I have seen so many people struggling to create sitecolumns and contenttypes in CAML format while developing sharepoint aplications. Some people spend days to complete these and some people spend hours depends on their knowledge on sharepoint, recently I have created webpart which will give sitecolumns and contenttypes in the form of CAML the main advantage of this is you will get complete features folder

you just need to deploy this into ur 12 hive features folder just install and activate

I just completed beta version of this webpart need to test and upload this into my blog

I hope this will save lot of time for each and every developer, min 1hour

First create your site columns using sharepoint GUI

Creating Site Columns

Site Actions

Modify All settings

Galleries

Site Columns

Create

Enter Column name , choose appropriate data type and click on create

This will create site column for you in sharepoint create as many you want

Creating Site Content Types

Site Actions

Modify All settings

Galleries

Site Content Types

Create

Provide Name and click on ok, this will create your content type

Adding columns to the content type

Go to content type gallery

Select content type the one which you created recently

Click on add from existing site columns

Select site columns and click on Ok

This will create content types for you

Now install RSM.Exporter webpart on respective webapplication

Add that webpart in any page of ur web application the webpart will dispaly as shown in fig



you just enter your site column feature name on site columns textbox and provide description for your feature and do same thing for site content types also and click on create sitecolumns and content types feature button

Now go to your "C://" drive and check "RsmmTechno" folder there you can find two folders one is for site columns and another one is for content types

So we have successfully completed creation of site columns and content types features now we need to create a page layouts based on content types

Steps to create page layouts :

Select content type which you want to attach to the page layout from content type drop downlist

Enter page layout feature name

Enter page layout name

Select fields which you want to render while creating page using page layout from the list box

Click on create page layout feature

Once again go to your "C://" drive and check "RsmmTechno" folder there you can find page layout feature along with site columns and content type as shown in below fig



Just open each and every folder you can find feature.xml file and element.xml files as shown in below fig







Now, Install and activate features



Create a page using page layout




I think this will help you guys a lot, no need of breaking heads what is the syntax for this sitecolumn content type and page layout etc...

Wednesday, February 24, 2010

Deploying Custom Resource files into "App_GloablResources"

Normally, when you create new webapplication in sharepoint the system will automatically copies all resources files from \\12\Config\Resources folder to respective webapplication Virtual directory "App_GlobalResources" folder this will work fine in normal scenarios, so in deployment you can just place your custom resources files into 12 hive Resources folder and create webapplication everything will work fine

But some times we may have to install some kind of upgradion packages with out affecting existing system which contains custom resource files? What would be the approach if we want to place all custom resources files into already created webapplication???

Options to deploy resources files in existing webapplication

1. We have to create deployment document and saying that copy and paste all the files into "App_GlobalResurces" folder

2. Create a separate feature and while activating that feature write a code to copy and paste all the files from feature folder to "App_GlobalResources" folder

Personally I prefer second approach

Please check below image to find out folder structre and where all resource files are placed



Just create feature with event receiver class in the FeatureActivated event write below code

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint.Utilities;
namespace RSMTechno.DeployGlobalResources
{
class DeployGlobalResources : SPFeatureReceiver
{

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

string sourcePath = string.Empty;
SPSite site = (SPSite)properties.Feature.Parent;
SPWeb web = site.OpenWeb();
//sourcepath is the path where all resource files are stored
sourcePath = string.Format("{0}\\FEATURES\\{1}\\", SPUtility.GetGenericSetupPath("TEMPLATE"), properties.Definition.DisplayName);
try
{
SPWebApplication webApp = web.Site.WebApplication;

foreach (SPUrlZone urlZone in webApp.IisSettings.Keys)
{
SPIisSettings settings = webApp.IisSettings[urlZone];
string destinationPath = Path.Combine(settings.Path.ToString(), "App_GlobalResources");
string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
File.Copy(filePath, Path.Combine(destinationPath, fileName), true);
}

}
}
catch (Exception)
{
throw new Exception("Failed to copy files");
}
}
}
}

Then build the application and deploy and activate the feature everything will work fine

Click here to download source code RSMTechno.DeployGlobalResourceFilesCode