Uncategorized

Creating List with VisualStudio 2010 in Sharepoint 2010 (simple code)


below I mentioned the simple code from my own labs.First It will check the particular list in the current site, by the help of SPContext.Current.Web , which show the current specs of  current web requested by the user.

CheckandCreateList(newweb)
 
{
SPWeb thisweb = SPContext.Current.Web;
using (SPWeb existingweb = thisweb.Webs[“JobData”])
{
try
{
//represent the list in sharepoint website//TryGetList Return the splistcollection

SPList jobdeflist = existingweb.Lists.TryGetList(“Job Definition”);if (jobdeflist != null) //here we check if SplistCollection is not null then list is already exits other wisenull then creae new list
{
string _status = “Job Definition List Already Exits”;

return;
}

//calling the method to creaet list
CreateList(existingweb);
string _status = “Job Definitions list have been added”;
}

catch (Exception ex){ }
}
}
void CreateList(SPWeb subweb)
{
//Creating the List

#region CreateList//add list to the SpWEb which we created on button click

Guid jobdefguid = subweb.Lists.Add(“Job Definition”,“Descrioption”, SPListTemplateType.GenericList);//this will return the unuiqe guid of list.//now adding column to the list we created.

SPList JobDef = subweb.Lists[jobdefguid];

JobDef.Fields.Add(“Minimum Salary”, SPFieldType.Currency, false);

JobDef.Fields.Add(“MaxSalary”, SPFieldType.Currency, true);

JobDef.Fields.Add(“JobDescription”, SPFieldType.Text, false);

JobDef.OnQuickLaunch =true;

JobDef.Update();

#endregion

#region Creating view

SPView vw = JobDef.Views[“All Items”];

vw.ViewFields.Add(“MinSalary”);

vw.ViewFields.Add(“MaxSalary”);

vw.ViewFields.Add(“JobDescription”);

vw.InlineEdit =“TRUE”;

vw.Update();

#endregion

#region

AddItem to the List

SPListItem newDef;

newDef = JobDef.Items.Add();

newDef[“Title”] = “Developer”;

newDef[“MinSalary”] = “40000”;

newDef[“MaxSalary”] = “80000”;

newDef[“JobDescription”] = “SharePoint or other Web Developer”;

newDef.Update();

#endregion

}

Add the below function where you required, to i.e. in webpart, in application page, etc.

Hope

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s