See Detail example at Using SPGridView to display list Data WebPart
Topic Covers :
1. How to display List data with SPGridView.
Steps:
1. Drag and drop SPGridView and SPDataSource from toolbox. (If you dont have them, right click on tabs and click on choose items and select GridView and SPDatasource).
2. Write the following in Code Behind for binding SPGridView with List Data.
protected void Page_Load(object sender, EventArgs e)
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList List = myWeb.Lists["Mylist"];
SPDataSource1.List = List ;
GridView .DataSource = SPDataSource1;
GridView .DataBind();
}
See Detail example at Using SPGridView to display list Data WebPart
Tuesday, May 27, 2008
Display List Data with SPGridView
Monday, April 28, 2008
Displaying List data in a Webpart
There are Various Options to display data from a list into a Webpart
1. Use Data View Webpart and display data in a grid view format.
2. Use Content Query WebPart
3. Use List View WebPart(Customize view in the List).
4. Create a Custom Webpart, and Use SPGridView or Gridview to Display data.
see example Here
Sunday, April 27, 2008
About Event Receivers and Event Handlers
Tere are two types of Events
Synchronous and Asynchronous Events.
* Synchronous events can trap an item, document library or site before it is deleted,added or updated.
eg, ItemAdding, ItemUpdating,ItemDeleting etc.
* Asynchronous events can trap an item, document library or site when it is added,deleted or updated.
eg, ItemAdded, ItemUpdated,ItemDeleted etc.
We create Event Recivers to handle these events. An event receiver basically, is a piece of managed code that is launched in response to an event that takes place within SharePoint.
SharePoint Object Model exposes several event classes inherited from Microsoft.SharePoint assembly. There are three main event classes:
SPItemEventReceiver
SPListEventReceiver
SPWebEventReceiver
Each class includes both synchronous and asynchronous methods to work with Item, List or Web Level.
Some of the Good Examples are on :
http://www.codeproject.com/KB/sharepoint/ExtendingSPS.aspx
http://msdn2.microsoft.com/en-us/library/ms453149.aspx
Sunday, February 10, 2008
Programmatically Adding Documnets in Document Library
Brief:
By Creating a ASP.Net file uploload contol and a submit button. You can add documents and Files from your local folder,directly into Sharepoint Document library by using SPFile class.
Lets get Started:
1. Create a ASP.Net Webapplication , and Add a Fileupload Control in that.
2. Add a button named "Upload" under the Fileupload Control.
3. Now double click the Upload Button and write the Site collection and site reference as below.
4. And Finally, Call the DocumnetsAttached method on the submit click.
protected void Button1_Click(object sender, EventArgs e)
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
DocumnetsAttached(myWeb);
}
Make sure you add a reference to Microsoft.Sharepoint.dll.
//The Function for uploading a File in a Document Library is Below:
public void DocumnetsAttached(SPWeb site)
{
if (FileUpload1.HasFile)
{
SPFolder folder = site.GetFolder("Document_Library_Name");
SPFileCollection files = folder.Files;
Stream fStream = FileUpload1.PostedFile.InputStream; //path of the file to upload
byte[] contents = new byte[fStream.Length];
fstream.position =0;
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
string Filename = FileUpload1.FileName;
string URL = SPContext.Current.Site.Url + "/Document_Library_Name/" + Filename;
SPFile currentFile = files.Add(URL, contents);
}
}
POpulating a Drop-Down with List Data
Steps:
1. Create a Drop-Down control,in a Webpart or Webapplication.
2. On the Load method, Add the following code.
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet ds = new DataSet();
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList list = myWeb.Lists["ListName"];
DataTable DTable_List = list.Items.GetDataTable();
DTable_List.TableName = "Table1";
ds.Tables.Add(DTable_List);
DropDownBGroup.DataSource = ds.Tables["Table1"];
DropDownBGroup.DataTextField = "FieldName";
DropDownBGroup.DataValueField = "FieldName";
DropDownBGroup.DataBind();
DropDownBGroup.SelectedIndex = 0;}
base.OnLoad(e);
}
}