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.

Wednesday, September 8, 2010

Access Denied - SharePoint Crawl Schedules

When you try and create or update a crawl schedule, you are prompted for a username and password, but for some strange reason your user credentials doesn't seem to work.

Upon examing the logs, you will find an Access Denied error.

This error occurs when the WSS_WPG group does not have the correct permissions to the c:\windows\tasks folder.

To solve this,
Open up the command prompt, type attrib –s %windir%\tasks, navigate to the c:\windows\tasks folder using windows explorer, right click and select the properties and then the securities tab, grant the WSS_WPG user Read, Write permissions to the folder, now reset IIS, using the following command, IISRESET \NOFORCE, then reset the folder back to the default view using the following command in the command prompt attrib +s %windir%\tasks.

For more info please see Microsoft article http://support.microsoft.com/kb/926959

Tuesday, August 24, 2010

Term Store Management Link missing

Error : "The required feature is not enabled for this column type"

You also do not see the Term Store Management link in site collection settings.

If you are using SharePoint 2010, and you have the Managed Meta Data service application you have to associate the service with your Web Application before you can use it.

Even though you might have followed a step by step guide, something went wrong and you recieve the following errors :

"The required feature is not enabled for this column type"

If you do come accoss this error, no worries, you will just have to activate this feature manually using the STSADM command line utility, found at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN"

You will have to run the following commands "stsadm -o activatefeature -id 7201d6a4-a5d3-49a1-8c19-19c4bac6e668 -url web application url -force".

I have found the id by looking in the following folder "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\MetaDataNav" and examing the "feature.xml" file.

And also run "stsadm -o activatefeature -id 73EF14B1-13A9-416b-A9B5-ECECA2B0604C -url web application url -force".

I have found the id by looking in the following folder "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\TaxonomyFieldAdded" and examing the "feature.xml" file.

This did the trick for me, hope that it did for you.

Monday, August 23, 2010

Office Web Applications Error

Word Web App cannot open this document due to an unknown error. If the problem persists, try opening the file in Microsoft Word.

Problem : This error usually occurs when you have not configured the service applications after you have installed the office web application package on your SharePoint Farm.

Resolution : Log into your SharePoint Central Administration site, navigate to your service application pages and create a service application for Word, PowerPoint and Excel. Once complete perform a IISRESET (not necessary) and try and re-open your document using Office Web Applications.

I hope this resolution has worked for you as it worked for me.