Wednesday, 14 May 2008

Validating Custom Web Part Properties - the proper way!

Adding validation to custom web part properties (the proper way!) Thanks Ken Josling for this info!

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.IO;
using System.Resources;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace YourNamespace
{

public class YourWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private bool invalidConfiguration;
private string exceptionMessage;
private ResourceManager resourceManager;

public YourWebPart()
{
invalidConfiguration = false;
exceptionMessage = string.Empty;
resourceManager =
new ResourceManager("YourNamespace.Resource", GetType().Assembly);
}

private void invalidateConfiguration(string message)
{
invalidConfiguration = true;
exceptionMessage = message;
}

protected void AddInitializeControl()
{
bool hasRights = ((ISecurableObject)SPContext.Current.Web).DoesUserHavePermissions((SPBasePermissions)Microsoft.SharePoint.SPPermissionGroup.WebDesigner);

if (!hasRights)
return;

string editLink = Microsoft.SharePoint.WebPartPages.ToolPane.GetShowExtensibleToolPaneEvent(string.Format(@"'{0}'", this.UniqueID));
this.Controls.Add(new LiteralControl(
string.Format(exceptionMessage + resourceManager.GetString("WebPartHelpMessage"), editLink.Replace(@"ExtensibleView", @"Edit"))
));

}

protected override void CreateChildControls()
{
//Add your validation logic here
if(x)
{
invalidateConfiguration(resourceManager.GetString("SomeErrorMessage"));
}
if (y)
{
invalidateConfiguration(resourceManager.GetString("SomeOtherErrorMessage"));
}

if (invalidConfiguration)
{
AddInitializeControl();
}
else
{
//Render Web Part Here
}
}

protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}

Monday, 12 May 2008

Trying to use an SPWeb object that has been closed or disposed and is no longer valid

Trying to use an SPWeb object that has been closed or disposed and is no longer valid

When you get the error message above it is most likely that you have got a reference to the SPWeb object by using the SPContext as shown below

SPWeb web = SPContext.Current.Web;

There is no problem with using this method but you need to make sure that you do not close or dispose this SPweb object becase the SPContext.Current.Web is used by the Page and all the controls on the page.

So you should not use this web object in Using statements also.

Wednesday, 30 April 2008

Working with SharePoint Groups

This is a nice article that talks about working with SharePoint groups programmatically.

The only problem I had was I had to replace the lines
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;

with SPRoleDefinition and added this role definition directly to the SPweb as shown below

SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);
def = web.RoleDefinitions["Full Control"];
roleAssignment.RoleDefinitionBindings.Add(def);
web.RoleAssignments.Add(roleAssignment);
web.Update();

http://www.emptycache.com/blog/2008/04/18/working-with-users-and-groups-in-sharepoint-2007/

Tuesday, 22 April 2008

SharePoint Admin webservice

The Admin web service that SharePoint exposes _vti_adm/Admin.asmx can be used to create new SharePoint Sites, etc..the catch is you cannot just append this webservice url to any sharepoint site but only to the web app that hosts central admin..

For instance if you have a number of SharePoint sites at the following ports
1. http://servername:1233/CollaborationSite
2. http://servername:4545/TeamSite
3. http://servername:1066 - Central Admin

To connect to the Admin webservice you cannot do something like http://servername:1233/_vti_adm_Admin/asmx but you have to use the port that Central Admin is hosted so it will be http://servername:1066/_vti_adm/Admin.asmx

Tuesday, 15 April 2008

Changing default text in Sharepoint

When you try to edit SharePoint pages like AccessDenied.aspx that sit in the Layouts folder in Sharepoints 12 folder hive you will notice that the text is not stored there.

They just reference resource files that sit under Inetpub under the location C:\Inetpub\wwwroot\wss\VirtualDirectories\portnumber\App_GlobalResources\wss.en-US.resx

So to change the Access Denied message to Access has been Denied or something like that you just need to change the value of the property accessDenied_pagetitle


Error: Access has been Denied

Thursday, 27 March 2008

Working with PDF's Dot Net

The PDFSharp open source Dot Net classes make working with PDF's in .Net very easy.
http://sourceforge.net/projects/pdfsharp

Friday, 7 March 2008

Create Page gives Access Denied error on Sharepoint

Came accross this problem where users with Full Control rights of a Sharepoint site were getting the Access Denied message when they clicked on the "create page" button.

The reason was that the users who create pages need to have read rights to the "Master Page Gallery"!