Wednesday, October 13, 2010

Creating Custom Web Part ToolPane

To create a custom toolpane is pretty simple, it's quite simple if you think about. All you need is an extra class that will create your toolpane.

I created a web part that had a custom toolpane that was used to display list in a site with checkboxes.

This toolpane allows users to select which list they wish to use on the site. Please see code below, and if you know of any better ways to do this please let me know:

WEB PART CODE EXAMPLE

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using SMC.ClassLibrary;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;


namespace OM.FILTER.JQUERY
{
[Guid("67e99305-67bc-444d-9f9d-0c3c53d3dfe8")]
[CLSCompliant(false)]
public class FilterJQuery : Microsoft.SharePoint.WebPartPages.WebPart
{

public FilterJQuery()
{

}

protected override void CreateChildControls()
{
base.CreateChildControls();
}

public override ToolPart[] GetToolParts()
{
List _toolParts = null;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite _spSite = new SPSite("url"))
{
using (SPWeb _spWeb = _spSite.OpenWeb())
{
CustomToolPart _customPart = new CustomToolPart("url");
_customPart.Title = "All Lists";

_toolParts = new List(base.GetToolParts());
_toolParts.Insert(0, _customPart);
}
}
});

return _toolParts.ToArray();
}


[CLSCompliant(false)]
[WebBrowsable(false),
Personalizable(PersonalizationScope.Shared),
Category("Custom Settings")]
public string _collectionSelected
{
get;
set;
}
}
}

TOOL PANE CODE EXAMPLE

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;
using SMC.ClassLibrary;

namespace OM.FILTER.JQUERY
{
class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
private CheckBoxList _spLists = new CheckBoxList();

private CheckBoxList chkBox
{
get { return _spLists; }
set { _spLists = value; }
}


private String _SiteURL;

public CustomToolPart(String _webURL)
{
this._SiteURL = _webURL;
}

protected override void CreateChildControls()
{
base.CreateChildControls();

BindToolPart();
}

private void BindToolPart()
{
chkBox.DataSource = Helper.GetAllListTitles(_SiteURL);
chkBox.DataTextField = "Title";
chkBox.DataValueField = "ID";
chkBox.DataBind();

FilterJQuery _jQuery = (FilterJQuery)this.ParentToolPane.SelectedWebPart;

if (_jQuery._collectionSelected != null)
{
List arrayList = Helper.SplitString(_jQuery._collectionSelected);

foreach (ListItem _item in chkBox.Items)
{
for (int i = 0; i < arrayList.Count; i++)
{
if (_item.Value == arrayList[i])
{
_item.Selected = true;
}
}
}
}
this.Controls.Add(chkBox);
}

public override void ApplyChanges()
{
base.ApplyChanges();

FilterJQuery parentWebPart = (FilterJQuery)this.ParentToolPane.SelectedWebPart;

string _iList = "";

foreach (ListItem _item in chkBox.Items)
{
if (_item.Selected)
{
_iList += _item.Value + ";";
}
}
parentWebPart._collectionSelected = _iList;

chkBox.EnableViewState = true;
EnableViewState = true;
this.SaveViewState();
this.SaveControlState();
}
}
}

Hope this helped you out as it sure helped me out a whole lot, let me know if you have any questions and I will try and respond to it asap.

Monday, October 4, 2010

Impersonating the Sharepoint System Account

Impersonating the SharePoint System Account is pretty simple. All you need is an the SPWeb object and you ready to go.

Here is how to do it.

using (SPWeb web = properties.OpenWeb())
{
var adminUser = web.AllUsers[@"SHAREPOINT\SYSTEM"];
var adminToken = adminUser.UserToken;

using (SPWeb site = new SPSite(web.Url, adminToken).OpenWeb())
{
/* Now running in the context of the system account */
}
}

And there it is, you can now run your code as the SharePoint System Account.

Storing Feature Values in Feature.XML file

Ever created a feature and wanted to deploy it on multiple servers without hard coding certain values?

It's kind of easy, you can use the feature.xml, namely the properties section to save values that you might want to change in future and then access them through code later, or even change them if need be.

In your feature.xml file, create a properties section, and add property tags for the relevant properties as shown below:




If you want to access this property you created you can do so by using the following code:

string _property = properties.Feature.Properties["SiteURL"].Value;

And that's it, how to access values stored in the feature.xml file.

Friday, October 1, 2010

FxCop Code Analysis

If ever you are deploying a solution, if it be a personal solution, private, or for a client. It helps you make sure that your solution conforms to Patterns and Practices as well as .NET Guidelines.

I recently found this out the hard way when one of my solutions did not pass the checks that FxCop performed.

In any event, this is a great tool for you to pick up any errors that testing might not, and to help you make sure that your solution adheres to best practices of Microsoft, and .NET Framework guidelines.

Documentation can be found here

Available for Download here

This tool has helped me immensely and I now see the error of my ways. I hope that you do to.