Sunday, June 14, 2009

Programmatically Adding SharePoint Group and Quick Add Group

Simple way to add a new custom group or custom quick add group programmatically.

Elements File Modification
Add the below 2 lines of code to your elements file under Elements > Module > File

 
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="AltsisWebParts" Url="_catalogs/wp">
<File Url="Srini.webpart" Type="" >
<Property Name="Group" Value="AltsisGroup" />
<Property Name="QuickAddGroups" Value="AltsisGroup" />
</File>
</Module>
</Elements>

Friday, June 12, 2009

Simple PunchIn PunchOut program using SPList



Scenario :The below code demoss a simple PunchIn / PunchOut program uisng SPList. Also depicts the Inserting data to sharepoinnnnnnt list using webpart. First step, see the below image output.
Steps :Step 1 : Create a custom list aaand add 3 columns to it. Username, InTime, OutTime.
Step 2 : Use the below code
Step 3 : Deploy the webpart and add the webpart to ur page.
Code :
  
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Punch
{
[Guid("c123a186-3795-4eda-b655-62c4dcedbeff")]
public class PunchTime : System.Web.UI.WebControls.WebParts.WebPart
{
private const string punchListName = "punchlist";
public PunchTime()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Button button = new Button();
button.Text = "Punch In";
button.Click += new EventHandler(button_Click);
this.Controls.Add(button);
}
void button_Click(object sender, EventArgs e)
{
SaveTimeLogs();
}
void SaveTimeLogs()
{
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;
SPListItemCollection listItemCol = web.Lists[punchListName].Items;
SPListItem item = listItemCol.Add();
item["UserName"] = user.Name.ToString();
item["InTime"] = DateTime.Now.ToShortTimeString();
item.Update();
}
}
}