Posts

Showing posts from December, 2013

Code for adding Content Type through Visual Studio (Windows Form- For SharePoint 2010)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.SharePoint; namespace Demo {     public partial class Form1 : Form     {         private const string siteurl = "http://win-5dlgan2f7s8:21905/CrimsonConsultingHR/Demo/";         public Form1()         {             InitializeComponent();         }   private void CreatecontentType_Click(object sender, EventArgs e)         {             using (var site = new SPSite(siteurl))             {                 var web = site.RootWeb;                 var empFieldName = web.Fields.Add("Employee", SPFieldType.Boolean, true);                 var employee = web.Fields.GetFieldByInternalName(empFieldName);                 employee.Group = "AayushContentType";                 employee.Update();                 var rateFieldName = web.Fields.Add("Salary/Rate", SPF

Code for creating list and column through Visual Studio(WIndows Form Application) for SharePoint 2010

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.SharePoint; namespace Demo {     public partial class Form1 : Form     {         private const string siteurl = "http://win-5dlgan2f7s8:21905/CrimsonConsultingHR/Demo/";         public Form1()         {             InitializeComponent();         }         private void button1_Click(object sender, EventArgs e)         {             using (var site = new SPSite(siteurl))             {                 var web = site.RootWeb;                 var ListId=web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);                 var List = web.Lists[ListId];                 List.OnQuickLaunch=true;                 List.ValidationFormula = "If([EmpId],TRUE,[Salary]<=50)";                 List.ValidationMessage = "The max salary for a Employee is

Code for adding HTML controls in Visual Web Part (For SharePoint 2010)

using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls. WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint. WebControls; using System.Web.UI.HtmlControls; using System.Drawing; namespace Sun_SharePointProject2.Gunjan {     [ToolboxItemAttribute(false)]     public class Gunjan : WebPart     {         //   public WebPart1()         //   {         //       AllowClose=false;         //   } /* Here i have taken 2 textbox and 2 label and 1 button for showing a table which will have 3 rows and 2 columns and will contain username and password button and textbox for each label and a button to submit */         private TextBox tx;         private TextBox t1;         private Label l;         private Label l1;         private Button bt;         HtmlTextWriter writer;         TableCell tc;         protected override void CreateChildControls()         {             tc = new TableCell();   

Code for sorting Grid View items on basis of Name or ID or any other column in Visual Studio WebPart( For SharePoint 2010)

using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data; using System.Collections; using System.Collections.Generic; using Microsoft.SharePoint; namespace TimerSharePoint.SortGridView { /* Class for get and set value of columns of a list in SharePoint. I have taken these 5 columns */     class Data     {         public string Name { get; set; }         public string Branch { get; set; }         public string Roll_No { get; set; }         public string Address { get; set; }         public string Mobile_No { get; set; }     }     class Datarollasc : IComparer<Data>     { /* When your field is of string data type(I have used String above in Data class) */         public int Compare(Data x, Data y)         {             return string.Compare(x.Roll_No, y.Roll_No);         } /* When your field is of Int data type(Although I haven't used String above in Data class but if u have int type then use this comm

Code for Composite web part(A dropdown will have initial value of list and on selecting value will show List)

using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; namespace GriddView.CompositeWebPart {     [ToolboxItemAttribute(false)]     public class CompositeWebPart : WebPart     {         private DropDownList listdropdown;         private GridView Itemgridview;         protected override void CreateChildControls()         {             listdropdown = new DropDownList();             listdropdown.AutoPostBack = true;             listdropdown.SelectedIndexChanged += new EventHandler(listdropdown_SelectedIndexChanged);             Controls.Add(listdropdown);                        Controls.Add(new LiteralControl("<br/><br/>"));             Itemgridview = new GridView();             Controls.Add(Itemgridview);         }         protected void listdropdown_SelectedIndexChanged(object sender,EventArgs e)

Building Simple Web Part(With HTML)

protected override void RenderContents (HtmlTextWriter writer) // Add a webpart (Don't select Sandboxed or Visual WebPart) //Override CreateChildControl method         {             writer.RenderBeginTag(HtmlTextWriterTag.H2);             writer.Write("Hello, world");             writer.RenderEndTag();         }

Code for viewing only that data which is selected from DropDown in Visual WebPart(SharePoint)

//Create a class to get and set value class Data     {         public string Name { get; set; }         public string Branch { get; set; }         public string Roll_No { get; set; }         public string Address { get; set; }         public string Mobile_No { get; set; }     }    //Code fro Page Load  protected void Page_Load(object sender, EventArgs e)         {             if (!IsPostBack)             {                 try                 {                     SPList info = SPContext.Current.Web.Lists["cust"];                     List<Data> data = new List<Data>();                     foreach (SPListItem item in info.Items)                     {                         Data d = new Data();                         d.Roll_No = item["Roll_No."].ToString();                         d.Name = item["Name"].ToString();                         d.Mobile_No = item["Mobile_No."].ToString();                         d.A
go to top image