Showing posts with label Webparts. Show all posts
Showing posts with label Webparts. Show all posts

Friday, March 25, 2011

SharePoint Custom WebPart Load JavaScript File

To load the JavaScript file (resides in the web site or layouts folder) in a custom web part, add the below set of code statements in the CreateChildControls() method.
using System.Web.UI.HtmlControls;
protected override void CreateChildControls()
{                    
base.CreateChildControls();

string jsFilePath = "/_layouts/MyProject/MyFolder/myJsFile.js";
HtmlGenericControl jsControl = new HtmlGenericControl("script");
jsControl.Attributes.Add("type", "text/javascript");
jsControl.Attributes.Add("src", jsFilePath);
this.Page.Header.Controls.Add(jsControl);
}

Wednesday, March 16, 2011

SharePoint Webpart Load UserControl (UserControl Webpart)

Creating a webpart to load the usercontrol instead of writing the html and business logic within the webpart itself.

Follow the below four steps to create a webpart with usercontrol.
1. Create a Asp.Net UserControl (.ascx file)
The .ascx file should inherit the code behind file as namespace.
<%@ Control Language="C#" AutoEventWireup="true" Inherits="MyProject.MyUserControls.MyClassName, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=681f114f2a212052" %>
2. Create .acsx.cs (code behind) class file as namespace for business logic
using System.Web.UI;
namespace MyProject.MyUserControls
{
public class MyClassName : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Code goes here...
}
}
}
3. Create a webpart

4. Load the UserControl in createchildcontrols() method.
Make sure that the .ascx file is deployed to layouts folder.
PlaceHolder ph;//placeholder to load the usercontrol
protected override void CreateChildControls()
{
ph = new PlaceHolder();
ph.ID = "profileUpdatePH";

string userControlFilePath = "~/_layouts/MyFolder/MyControl.ascx";
MyUserControls.MyClassName myControl = Page.LoadControl(userControlFilePath) as MyUserControls.MyClassName;

ph.Controls.Add(myControl);
this.Controls.Add(ph);
}

Tuesday, March 15, 2011

SharePoint Webpart Custom Properties with Default Value

Most of the sharepiont custom development involves creating custom webparts. There could be scenarios where some of the values like connection string, site url, list/library name are configurable by Admin/End User and changeble depending on the requirement. To achieve this for SharePoint webpart, the values can be configured using webpart properties. Any number of webpart properties can be added to the webpart with one or more categories to differenciate. Property value can be modified/changed by modifying the shared webpart.

Webpart property default value can be set using DefaultValue property, as shown below. Note that to set the default value, the value should be stored in a constant variable, otherwise it doesn't work properly.
//Namespaces required

using System.ComponentModel;

using System.Web.UI.WebControls.WebParts;



const string const_listName = "ListName";

private string _listName = const_listName;

[Personalizable(PersonalizationScope.Shared)]

[WebBrowsable(true)]

[Category("My Category")]

[WebDisplayName("List Name")]

[WebDescription("Provide list name to get all the items.")]

[DefaultValue(const_listName)]

public string ListName

{

get

{

return _listName;

}

set 

{

_listName= value; 

}

}

Thursday, February 24, 2011

Webpart to render New Form dynamically for a list item

To generate/render a new form in a webpart dynamically to create/add a list item. Where the field name, type of each field will be fetched and rendered respectively. It holds the full control on all the properties of a column.
//Create the table object that we are going to add the rows and cells to for our data entry form
Table tbl = new Table();
tbl.CellPadding = 0;
tbl.CellSpacing = 0;

// Get the site that this web part is running on.
SPWeb spWeb = SPContext.Current.Web;

// Get the list we are going to work with
SPList spList = spWeb.Lists["MyList"];

// Loop through the fields
foreach (SPField spField in spList.Fields)
{
// See if this field is not hidden
if (!spField.Hidden && !spField.ReadOnlyField && spField.Type != SPFieldType.Attachments)
{
// Create the label field
FieldLabel fieldLabel = new FieldLabel();
fieldLabel.ControlMode = SPControlMode.New;
fieldLabel.ListId = spList.ID;
fieldLabel.FieldName = spField.InternalName;

// Create the form field
FormField formField = new FormField();
formField.ControlMode = SPControlMode.New;
formField.ListId = spList.ID;
formField.FieldName = spField.InternalName;

// Add the table row
TableRow tblRow = new TableRow();
tbl.Rows.Add(tblRow);

// Add the cells
TableCell tblLabelCell = new TableCell();
tblRow.Cells.Add(tblLabelCell);
TableCell tblControlCell = new TableCell();
tblRow.Cells.Add(tblControlCell);

// Add the control to the table cells
tblLabelCell.Controls.Add(fieldLabel);
tblControlCell.Controls.Add(formField);

// Set the css class of the cell for the SharePoint styles
tblLabelCell.CssClass = "ms-formlabel";
tblControlCell.CssClass = "ms-formbody";
}
}

// Create the save button
SaveButton btnSave = new SaveButton();
btnSave.ControlMode = SPControlMode.New;
btnSave.ListId = spList.ID;

// Create the row for the save button
TableRow tblButtonRow = new TableRow();
// Create the cell for the save button
TableCell tblButtonCell = new TableCell();

tblButtonCell.ColumnSpan = 2;
tblButtonRow.Cells.Add(tblButtonCell);
tbl.Rows.Add(tblButtonRow);

// Add the table to the web part controls collection
this.Controls.Add(tbl);