Friday, October 24, 2008

How to register your custom events in MOSS 2007

You can register your custom MOSS event handlers using a few techniques. I have always been a fan of a simple Console application which will do the trick. The pre-requisites are:
1) Ensure that the assembly implementing the custom event handlers in strongly typed 
2) Ensure that your custom event handlers strongly typed assembly has been registered in the GAC of the server (or on each server on a web farm).

Once you have checked the pre-requisites you can write a Console application to do your job. An example is as below:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace trusharWebsite.Website.RegisterEventHandlers
{
// Console application to register custom event handlers on a specific list called "Comments" on the website
class Program
{
static void Main(string[] args)
{
SPSite curSite = new SPSite("http://trushar/trusharBlog");
SPWeb curWeb = curSite.OpenWeb();

SPList commentsList = curWeb.Lists["Comments"];

string asmName = "trusharWebsite.WebSite.EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=342fb345e6f432a";
string className = "trusharWebsite.WebSite.EventHandler.AddmyItemHandler";

commentsList.EventReceivers.Add(SPEventReceiverType.ItemAdding, asmName, className);
commentsList.EventReceivers.Add(SPEventReceiverType.ItemUpdating, asmName, className);
}
}
}

Thursday, October 23, 2008

Update WSS 3.0 User Profile Programmatically

In WSS 3.0, there is a special list ‘User Information List’ for user’s profile information. When a user is added to the Sharepoint site, a list item is automatically created in this list. To update the user’s information in ‘User Information List’, for instance, to update the user’s photo, I am using following code and it works fine:

 

SPList list = web.Lists["User Information List"];

SPUser u = web.SiteUsers[“domain\\user1”];

.SPListItem it = list.Items.GetItemById(u.ID);

 

it[“Picture”] =”http://sharepointApp/sites/wss/photos/abc.jpg”;

it.Update();

 Also, you are able to add a custom field to user profile like this:

 SPList list = web.Lists["User Information List"];

if (!list.Fields.ContainsField("Favorites"))
{
         list.Fields.Add("Favorites", SPFieldType.Text, false);
         list.Update();
}

Saturday, October 18, 2008

Adding your own Administration Pages to Site Settings or Central Administration

You can make your custom SharePoint Applications look more like a part of the SharePoint environment by adding any Administration Tasks (ie. settings pages) that you might require into the Admin Console or Site Settings areas.

For this you need a Feature and a definition of the new menu item to be added and where it links to.

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="34251A34-3B03-4149-94A3-A04C5F99E251"
Title="My Administration Link"
Description="Link to administration page."
Version="12.0.0.0"
Scope="Web"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>

Element.xml file is as below :

<?xml version="1.0" encoding="utf-8"?>
<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="ManageMySettings"
GroupId="SiteCollectionAdmin"
Location="Microsoft.SharePoint.SiteSettings"
RequireSiteAdministrator="TRUE"
Sequence="45"
Title="Manage My Settings">
<UrlAction
Url="_layouts/MyAppSettings.aspx" />
</CustomAction>
</Elements>

This elements.xml file adds a link to the Site Settings Page (by setting the Location attribute), under the Site Collection Admin area (specified by the Group ID) and requiring Site Administrator Access to get to. The UrlAction element specifies where the link goes (in this case to an aspx page in _layouts).

I can also insert links into the Central Admin area if needed just by specifying a different CustomAction:

<?xml version="1.0" encoding="utf-8"?>
<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomActionGroup
Id="Mine"
Location="Microsoft.SharePoint.Administration.ApplicationManagement"
Title="My Applications"
Sequence="100" />
<CustomAction
Id="MyAppSettings"
GroupId="Mine"
Location="Microsoft.SharePoint.Administration.ApplicationManagement"
Sequence="11"
Title="My App Settings">
<UrlAction
Url="/_admin/AppSettings.aspx" />
</CustomAction>
</Elements>



SharePoint 2007 Forms Authentication with sqlserver Membership

Here is the sharepoint 2007 form authentication for sqlserver membership.
I have read this article and it is good for form authentication.

http://weblog.vb-tech.com/nick/archive/2006/06/14/1617.aspx

http://www.andrewconnell.com/blog/articles/HowToConfigPublishingSiteWithDualAuthProvidersAndAnonAccess.aspx

http://weblog.vb-tech.com/nick/

http://www.chandima.net/Blog/archive/2007/02/15/how-to-setup-and-configure-forms-based-user-administration-feature-release-1-0-beta.aspx

http://www.codeproject.com/KB/sharepoint/FBA.aspx

Friday, October 17, 2008

Validating Date field in SharePoint through Javascript

Just put the following code in NewItem.aspx :

function PreSaveAction()
{
var date1 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Contract Date");

var date2 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Contract End Date");

var arrDate1 = date1.value.split("/");

var useDate1 = new Date(arrDate1[2], arrDate1[1]-1, arrDate1[0]);

var arrDate2 = date2.value.split("/");

var useDate2 = new Date(arrDate2[2], arrDate2[1]-1, arrDate2[0]);

if(useDate1 > useDate2)
{
alert("The end date cannot happen earlier than the start date");

return false; // Cancel the item save process
}

return true; // OK to proceed with the save item }

How to get details error page instead of "An unexpected error has occurred" in MOSS 2007?

Debugging SharePoint can be problematic at times, it does like to hide debugging information from you. Whenever any error occurs in application it just display a page with error message “An unexpected error has occurred” with nothing written to log files, trace or the event log.
To get detail asp.net error page, you just need to do couple of changes in web.config file

SafeMode MaxControls=“200“ CallStack=“false“…
to…
SafeMode MaxControls=“200“ CallStack=“true“…

You will also need to set custom errors to 'Off' .

customErrors mode=“Off“


You will no longer see the “An unexpected error has occurred” error page and instead you get a lovely ’standard ASP.Net error page’ with the stack trace and everything…development has got that little bit easier!!

Saturday, October 11, 2008

STSADM Operation Command

Here is a link for STSADM Operation command that can be useful in the Moss 2007 and wss 3.0.

http://blogs.technet.com/josebda/archive/2008/03/15/complete-reference-of-all-stsadm-operations-with-parameters-in-moss-2007-sp1.aspx