Monday, November 1, 2010

SharePoint Basic Source Codes

Get Discussion names and their item count

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPListCollection disccontrol = web.Lists;
DataTable dt = new DataTable();
dt.Columns.Add("DiscName", typeof(string));
dt.Columns.Add("DiscCount", typeof(string));
var tot = 0;
foreach (SPList dlist in disccontrol)
{
if (dlist.BaseTemplate == SPListTemplateType.DiscussionBoard)
{
var DiscName = dlist.Title;
var DiscCount = dlist.Folders.Count;
tot += DiscCount;
dt.Rows.Add(DiscName, DiscCount);
}
}
discGrid.DataSource = dt;
discGrid.DataBind();
totItems.Text = tot.ToString();


Get Details from a list using LINQ

var dc = new SPLinqDataContext(SPContext.Current.Web.Url);
var conts = dc.GetList("contacts");
var myquery = from c in conts
select new
{
Name=c.Title,c.Email,c.Mobile
};
mygrid.DataSource = myquery;
mygrid.DataBind();


Get Library Details

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPListCollection ls = web.Lists;
var Name = "";
var Count = 0;
var Total = 0;
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Count", typeof(string));
foreach (SPList list in ls)
{
if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
{
Name = list.Title;
Count = list.Items.Count;
table.Rows.Add(Name, Count);
Total += Count;
}
}
table.Rows.Add("Total", Total);
dispGrid.DataSource = table;
dispGrid.DataBind();


Get file details from a particular library

SPSite currSite = SPContext.Current.Site;
SPWeb myWeb = currSite.OpenWeb();
SPList mylist= myWeb.Lists["mydoc"];
SPListItemCollection col = mylist.Items;
var FileName="";
var User="";
DataTable table = new DataTable();
table.Columns.Add("FileName", typeof(string));
table.Columns.Add("User", typeof(string));
foreach (SPListItem item in col)
{
FileName = item.Name;
User = item.File.Author.ToString();
table.Rows.Add(FileName, User);
}
myGrid.DataSource = table;
myGrid.DataBind();


Get details from a library using CAML query

string qry = "Your CAML query for library";
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPList list = web.Lists["mydoc"];
SPQuery query = new SPQuery();
query.Query = qry;

DataTable dt = new DataTable();
dt = list.GetItems(qry).GetDataTable();
myGrid.DataSource = dt;
myGrid.DataBind();


Get details of a library based on user selected from a dropdown box

protected SPSite site;
protected SPWeb web;
protected SPList myList;
protected SPListItemCollection itemColl;
protected DataTable dt;
protected SPUserCollection users;
protected void Page_Load(object sender, EventArgs e)
{
site = SPContext.Current.Site;
web = site.OpenWeb();
myList = web.Lists["myDoc"];
dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Count", typeof(string));
if (!IsPostBack)
{
users = web.SiteUsers;
DDList.Items.Clear();
foreach (SPUser s in users)
{
DDList.Items.Add(s.ToString());
}
bindData();
}
}
protected void bindData()
{
itemColl = myList.Items;
var fname = "";
var auth = "";
foreach (SPListItem item in itemColl)
{
fname = item.Name;
auth = item.File.Author.ToString();
if(auth.Equals(DDList.SelectedItem.ToString()))
dt.Rows.Add(fname,auth);
}
myGrid.DataSource = dt;
myGrid.DataBind();
}
protected void DDList_SelectedIndexChanged(object sender, EventArgs e)
{
bindData();
}


Assigning total count to a footer

if (e.Row.RowType == DataControlRowType.DataRow)
totalItems += Convert.ToInt32(e.Row.Cells[1].Text);
else if (e.Row.RowType == DataControlRowType.Footer)
e.Row.Cells[1].Text=totalItems.ToString();


Save data to field from webpart

SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPList myList = web.Lists["details"];
SPListItem listItem = myList.Items.Add();
listItem["Name"] = txtName.Text;
listItem["Age"] = txtAge.Text;
listItem["Qualification"] = txtQualification.Text;
listItem.Update();
myList.Update();

No comments:

Post a Comment