I am going to write a few methods showing some of the basic operations which can be performed using object model.
Microsoft provides lot of API's for working with object model.
Examples are- SPSite,SPWeb,SPList etc..
All these api's are availbale in Microsoft.Sharepoint.dll .so add it as a reference and insert in code
using Micosoft.Sharepoint namespace.
create a new project in visual studio.
(My suggestion is to create a Empty sharepoint project from sharepoint 2010 template).
Then add a visual web part there.
To get the current context -
using(SPSite site =new SPSite(SPContext.current.site.URL))
to get a current web inside the site-
using(SPWeb web =new SPWeb(site.OpenWeb())
Let's say I have a Student entity class.
[serializable]
Public class Student
{
public string Name {get;set:}
public string Email {get;set;}
}
Now I want to get all the list items stored in student list of sharepoint site.
public List<Student> GetAllStudents()
{
List<Student> lstStudent = new List<Student>();
SPList list = web.Lists["Student"];
SPListItemCollection collectionItem = list.Items; //Get all items
//SPListItemCollection collectionItem = list.getItems(query) //If CAML query is used
foreach (SPItem item in collectionItem)
{
lstStudent .Add(GetStudent(item));
}
}
private Student GetStudent(SPItem item)
{
return new Student
{
Name= (string)item.Name,
Email = (string)item["Email"]
};
}
Lets say I want to add in student list.
We will write a method like
public void AddStudent(Student objstudent) //Passing object of student class
{
SPList list = web.Lists["Student"];
SPItem itemstudent;
itemstudent=list.AddItem();
itemstudent["Name"]=objstudent.Name;
itemstudent["Email"]=objstudent.Email;
itemstudent.update();
}
I will be writing few more examples..
Microsoft provides lot of API's for working with object model.
Examples are- SPSite,SPWeb,SPList etc..
All these api's are availbale in Microsoft.Sharepoint.dll .so add it as a reference and insert in code
using Micosoft.Sharepoint namespace.
create a new project in visual studio.
(My suggestion is to create a Empty sharepoint project from sharepoint 2010 template).
Then add a visual web part there.
To get the current context -
using(SPSite site =new SPSite(SPContext.current.site.URL))
to get a current web inside the site-
using(SPWeb web =new SPWeb(site.OpenWeb())
Let's say I have a Student entity class.
[serializable]
Public class Student
{
public string Name {get;set:}
public string Email {get;set;}
}
Now I want to get all the list items stored in student list of sharepoint site.
public List<Student> GetAllStudents()
{
List<Student> lstStudent = new List<Student>();
SPList list = web.Lists["Student"];
SPListItemCollection collectionItem = list.Items; //Get all items
//SPListItemCollection collectionItem = list.getItems(query) //If CAML query is used
foreach (SPItem item in collectionItem)
{
lstStudent .Add(GetStudent(item));
}
}
private Student GetStudent(SPItem item)
{
return new Student
{
Name= (string)item.Name,
Email = (string)item["Email"]
};
}
Lets say I want to add in student list.
We will write a method like
public void AddStudent(Student objstudent) //Passing object of student class
{
SPList list = web.Lists["Student"];
SPItem itemstudent;
itemstudent=list.AddItem();
itemstudent["Name"]=objstudent.Name;
itemstudent["Email"]=objstudent.Email;
itemstudent.update();
}
I will be writing few more examples..
No comments:
Post a Comment