Thursday, March 31, 2011

Create Silverlight Accordion Control in SharePoint

This article will explain how to create Silverlight accordion control in SharePoint context.
Expected Output:




















Steps to create Silverlight application to be hosted on SharePoint Silverlight webpart

1. Create Silverlight application.
2. Create an entity class. This class will contain property as TeamName, Captain and CaptainImage of teams.






3. MainPage.xaml

























4. MainPage.xaml.cs


















































5. Data retrieve from SharePoint List

Tuesday, March 29, 2011

Programmatically attach workflow to List Item of SharePoint List

In this article, I will explain how to programmatically attach workflow to list item of SharePoint list. Workflow can be attached to a list while adding new item or updating the item.
Following method can be used to attach workflow to list item programmatically
Public void AttachWorkflowToListItem (string ListName, int ListItemID, SPSite oSite, SPWeb oWeb)
{
oWeb.AllowUnsafeUpdates = true;
SPList _ListName = oWeb.Lists [ListName];
SPListItem _ListItem = _ ListName.GetItemById (Convert.ToInt32 (ListItemID));
//Creates and starts a new workflow instance on a specified list item
SPWorkflowManager _manager = oSite.WorkflowManager;
// collect all workflows which are associated with the List
SPWorkflowAssociationCollection _associationCollection = _ ListName.WorkflowAssociations;
LookupAndStartWorkflow (_ListItem, _manager, _associationCollection, "WorkflowID");
}
Private static void LookupAndStartWorkflow (SPListItem ListItem, SPWorkflowManager manager, SPWorkflowAssociationCollection AssociationCollection, string WorkflowId)
{
//iterate workflow associations and lookup the workflow to be started
Foreach (SPWorkflowAssociation association in AssociationCollection)
{
//if the workflow association matches the workflow we are looking for,
//get its association data and start the workflow

if (association.Name.Equals(WorkflowId))
{
//get workflow association data
String data = association.AssociationData;
//start workflow
SPWorkflow wf = manager.StartWorkflow (ListItem, association, "open tag Data close tag Data", true);
break;
}
}
}

Monday, February 28, 2011

Programmatically add web part to SharePoint 2010 web part page

In this article, I will explain how to programmatically add web part to sharepoint web part page.
1. Select SharePoint empty project template then add Module.
2. Right click on Module ,then add ->Existing Item
Browse to “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\sts” select default.aspx page.
3. Update the elements manifest to tell where to find the custom file and where it needs to deploy.
?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Module Name="Home" Url="" Path="">
File Path="Home\default.aspx" Url="default.aspx" IgnoreIfAlreadyExists="TRUE" Type="Ghostable">
AllUsersWebPart WebPartOrder="1"
WebPartZoneID="Left">
![CDATA[
webParts>
webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
metaData>
importErrorMessage>Cannot import this WebPart.
/importErrorMessage>
/metaData>
data>
properties>
property name="HelpUrl" type="string" />
property name="AllowClose" type="bool">True /property>
property name="ExportMode" type="exportmode">All /property>
property name="Hidden" type="bool">False /property>
property name="AllowEdit" type="bool">True /property>
property name="Direction" type="direction">NotSet /property>
property name="TitleIconImageUrl" type="string" />
property name="AllowConnect" type="bool">True /property>
property name="HelpMode" type="helpmode">Modal /property>
property name="CustomProperties" type="string" null="true" />
property name="AllowHide" type="bool">True /property>
property name="Description" type="string">Silverlight webpart /property>
property name="CatalogIconImageUrl" type="string" />
property name="MinRuntimeVersion" type="string" null="true" />
property name="ApplicationXml" type="string" />
property name="AllowMinimize" type="bool">True /property>
property name="AllowZoneChange" type="bool">True /property>
property name="CustomInitParameters" type="string" null="true"/>
property name="Height" type="unit">500px /property>
property name="Width" type="unit">
property name="Title" type="string">Home /property>
property name="TitleBar" type="string">Home /property>
property name="TitleUrl" type="string" >Home /property>
property name="Url" type="string">
/_LAYOUTS/ClientBin/SilverlighApplicationDemo.xap
/property>
property name="WindowlessMode" type="bool">True
/properties>
/data>
/webPart>
/webParts> ]]>
/AllUsersWebPart>
/File>
/Module>
/Elements>
How to uninstall an assembly from GAC (Global assembly Cache)
In this article, I will explain different ways to uninstall an assembly from GAC
1. Using the Windows interface
a. Navigate to the GAC, which is located at %System Drive%\Windows\Assembly.
b. Right-click assembly file that is included in your application, click Uninstall, and then click yes to confirm.
2. Using the command line
a. Click Start, then point to Command Prompt, and run it as administrator.
b. At the command prompt, Navigate to following path:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"
c.Enter following command gacutil /u "fully qualified assembly name".
In this command, fully qualified assembly name is the name of the assembly to uninstall from the GAC.
For e.g.
gacutil /u "FirstApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef4b5fc568888"
Here FirstApplication is assembly name

Monday, January 31, 2011

Programmatically create custom default page for SharePoint 2010 Team site

In this article, I will explain how to programmatically create custom default page and set it to root level.

Step 1. Select SharePoint empty project template and then add Module.
Step 2.Update the elements manifest to tell where to find the custom file /Page and where it needs to deploy.

. ?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Module Name="Home" Url="" Path="">. ?xml version="1.0" encoding="utf-8"?>
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Module Name="Home" Url="" Path="">
File Path="Home\default.aspx" Url="default.aspx" IgnoreIfAlreadyExists="TRUE" Type="Ghostable">
/File>
/Module>
/Elements>


Step 3. Add event handler to the feature.
SPWeb oWeb = null;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
oWeb = (SPWeb)properties.Feature.Parent;
SPFolder rootfolder = oWeb.RootFolder;
rootfolder.WelcomePage = "default.aspx";
rootfolder.Update();
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
oWeb = (SPWeb)properties.Feature.Parent;
SPFolder rootfolder = oWeb.RootFolder;
rootfolder.WelcomePage = "SitePages/Home.aspx";
rootfolder.Update();
}

Monday, January 24, 2011

Method to get Current logged In user using Client object model

In this article, I will show method used to get current logged In user using client object model.

private User GetCurrentLoggedInUser()
{
Web oWebSite = ClientContext.Current.Web;
ClientContext.Current.Load(oWebSite);
ClientContext.Current.ExecuteQuery();

User oUser = oWebSite.CurrentUser;
ClientContext.Current.Load(oUser);
ClientContext.Current.ExecuteQuery();
return oUser;
}
This method return User object. so to get loggedIn user create User class object and call this method.
Private User oUser = GetCurrentLoggedInUser ();

Create Silverlight Applications to Access SharePoint 2010 List Data using Client Object Model

In this article, I will create a simple Silverlight 4 application that displays a SharePoint 2010 list data inside a datagrid control. To do so, I need to follow given steps:-
1. Create a Silverlight Application Project.
2. Add code to access SharePoint list data.
3. Deploy the solution.
4. Test the solution.

Create a Silverlight Application Project

1. Start VS 2010, Click File, point to New Project, then select Silverlight Application.

2. At the top of the screen, select .NET Framework 3.5, type SilverlightApplicationDemo in the Name box and then click ok.

3. The New Silverlight Application dialog box appears, and then unchecks the “Host the Silverlight application in a new web site” option. We don’t need it even though you can use this to host your silverlight application and select Silverlight 4 as silverlight version.

4. Add reference to the SharePoint silverlight client object model. For doing so in solution explorer, right click on Add References.

5. Next, browse to “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin”, then select Microsoft.SharePoint.ClientSilverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll, and click OK.


Add Code to access SharePoint list data

1. To view data, drag a DataGrid control from the toolbox onto existing grid in the MainPage.xaml silverlight Designer. And set AutoGenerateColumns to true.
2. Open MainPage.xaml.cs and then add following statement at the top of the file.
using Microsoft.SharePoint.Client;

3. Add the following class before the MainPage class.
public class EmpInfo
{
// Public properties
public string EmpID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
4. ListItemCollection gempItemCol
ObservableCollection< EmpInfo > gEmpInfo= new ObservableCollection< EmpInfo >();

public MainPage()
{
InitializeComponent();
GetEmpData();
}
private void GetEmpData ()
{
try
{
ClientContext Context = new ClientContext(ApplicationContext.Current.Url);
Context.Load(Context.Web);
List _emps = Context.Web.Lists.GetByTitle(“emp”);
Context.Load(_channels);
CamlQuery _query = new Microsoft.SharePoint.Client.CamlQuery();
string _camlQueryXml = @"View> /View>";
_query.ViewXml = _camlQueryXml;
gempItemCol =_emps.GetItems(_query);
Context.Load(gempItemCol);
Context.ExecuteQueryAsync(OnRequestSucceeded, null);
}
catch (Exception ex)
{;}
}
private void OnRequestSucceeded (Object sender, ClientRequestSucceededEventArgs args)
{
try
{
Dispatcher.BeginInvoke(BindEmpsData);
}
catch (Exception ex)
{; }
}

private void BindEmpsData ()
{
try
{
foreach (ListItem _item in gempItemCol)
{
_ EmpID= _item.FieldValues["EmpID"] as FieldLookupValue;//if taken lookup field
String _strEmpID;
if (_EmpID == null)
_ strEmpID = string.Empty;
else
_ strEmpID = _ EmpID.LookupValue;

gEmpInfo.Add(new EmpInfo()
{
Name = Convert.ToString(_item["Name"]),
EmpID = Convert.ToString(_item.Id),
Address== Convert.ToString(_item["Address "])
});
}
lstEmployee.ItemsSource = gEmpInfo;//Bind DataGrid
}
catch (Exception ex)
{ ; }
}

Deploy the solution

1. To deploy the solution onto SharePoint, the resulting .xap file of silverlight application must be present in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin folder.
2. To do so there are two ways
a) You can change the output path to “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin”.
b) Manual copy and paste it in ClientBin folder.
3. Build the solution and follow any of the above approach.

Test the solution

1. Navigate to the SharePoint site.
2. Select Edit Page icon.
3. Click Insert and then click web part, select Media and Content from web part list and then click add.
4. In the silverlight web part dialog box type /_layouts/ClientBin/ SilverlightApplicationDemo.xap as URL and then click ok.