Sunday, October 23, 2011

How to hide/show the SharePoint 2010 ribbon according to SharePoint group

In this post, I will explain how to hide/show the SharePoint 2010 ribbon according to SharePoint group. Use the following code to do so
Sharepoint:SPSecurityTrimmedControl runat="server" Permissions="EditListItems">
!—Ribbon code-->
/Sharepoint:SPSecurityTrimmedControl>

How to insert data programmatically in SharePoint list using object model

In this post I will explained, how we can programmatically insert data in SharePoint list using SharePoint object model.
Step 1: Create a SharePoint list say Employee_Details with following field
a. EmployeeName
b. EmployeeID
c. JoiningDate
Step 2: Create SharePoint empty project, add visual web part and design the following UI

Step 3: Add the following code in .ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace VWPListOperation.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnAdd_Click(object sender, EventArgs e)
{
SPSite mysite = SPContext.Current.Site;
SPWeb myweb = mysite.OpenWeb();
SPListItemCollection listItemsCol = myweb.Lists["Employee_Details"].Items;
SPListItem item = listItemsCol.Add();
item["EmployeeName"] = txtEmployeeName.Text;
item["Title"] = txtDesi.Text;
item["EmployeeID"] = Convert.ToInt32(txtEmployeeID.Text);
if (!JoiningDateTimeControl.IsDateEmpty)
{
item["JoiningDate"] = Convert.ToDateTime(JoiningDateTimeControl.SelectedDate);
}
else
{
item["JoiningDate"] = DateTime.Now;
}
item.Update();
lblMsg.Visible = true;
}

protected void btnCancel_Click(object sender, EventArgs e)
{
txtEmployeeName.Text = String.Empty;
txtDesi.Text = String.Empty;
txtEmployeeID.Text = String.Empty;
//JoiningDateTimeControl.SelectedDate = DateTime.Now;
lblMsg.Visible = false;
}
}
}
Step 4: Following is the result