Below is the code: using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using System.Web; using System.Diagnostics;
namespace CreateDocID
{
class CreateDocumentID : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
EventLog LogToSystem = new EventLog();
//LOG ENTRY TO THIS FUNCTION
LogToSystem.WriteEntry("Entered ItemAdded in CreateDocumentID",
EventLogEntryType.Information);
//initialise custom class
ConfigurationList ConfigurationList = new
ConfigurationList();
base.DisableEventFiring();
try
{
//Set the source of the Errors that are logged to the Event
log.
LogToSystem.Source = "CreateDocID.ItemAdding.Override";
using (SPWeb web = properties.OpenWeb())
{
ConfigurationList.theSite = web.Url;
string sDocumentLibraryName =
ConfigurationList.GetValue("Document Library");
string sBU = ConfigurationList.GetValue("BU");
string sDocIDField =
ConfigurationList.GetValue("Document ID Field");
string listTitle = properties.ListTitle;
//get the type of list that this item is being created
in
if (listTitle == sDocumentLibraryName)
{
// Set the unique id value on the item depending on
the list type.
if
(properties.AfterProperties["ContentType"].ToString() == "Specification")
{
properties.AfterProperties[sDocIDField] = sBU +
"SPEC" + GetUniqueDateId() + GetUniqueId(web);
}
else if
(properties.AfterProperties["ContentType"].ToString() == "General")
{
properties.AfterProperties[sDocIDField] = sBU +
"GEN" + GetUniqueDateId() + GetUniqueId(web);
}
}
}
}
catch (Exception ex)
{
LogToSystem.WriteEntry(ex.StackTrace,
EventLogEntryType.Error);
}
finally
{
//LOG EXIT FORM THIS FUNCTION
LogToSystem.WriteEntry("Exiting ItemAdded in
CreateDocumentID", EventLogEntryType.Information);
base.EnableEventFiring();
}
base.ItemAdded(properties);
}
public override void ItemAdding(SPItemEventProperties properties)
{
EventLog LogToSystem = new EventLog();
//LOG ENTRY TO THIS FUNCTION
LogToSystem.WriteEntry("Entered ItemAdding in CreateDocumentID",
EventLogEntryType.Information);
//initialise custom class
ConfigurationList ConfigurationList = new
ConfigurationList();
base.DisableEventFiring();
try
{
//Set the source of the Errors that are logged to the Event
log.
LogToSystem.Source = "CreateDocID.ItemAdding.Override";
using (SPWeb web = properties.OpenWeb())
{
ConfigurationList.theSite = web.Url;
string sDocumentLibraryName =
ConfigurationList.GetValue("Document Library");
string sBU = ConfigurationList.GetValue("BU");
string sDocIDField =
ConfigurationList.GetValue("Document ID Field");
string listTitle = properties.ListTitle;
//get the type of list that this item is being created
in
if (listTitle == sDocumentLibraryName)
{
// Set the unique id value on the item depending on
the list type.
if
(properties.AfterProperties["ContentType"].ToString() == "Specification")
{
properties.AfterProperties[sDocIDField] = sBU +
"SPEC" + GetUniqueDateId() + GetUniqueId(web);
}
else if
(properties.AfterProperties["ContentType"].ToString() == "General")
{
properties.AfterProperties[sDocIDField] = sBU +
"GEN" + GetUniqueDateId() + GetUniqueId(web);
}
}
}
}
catch (Exception ex)
{
LogToSystem.WriteEntry(ex.StackTrace,
EventLogEntryType.Error);
}
finally
{
//LOG EXIT FORM THIS FUNCTION
LogToSystem.WriteEntry("Exiting ItemAdding in
CreateDocumentID", EventLogEntryType.Information);
base.EnableEventFiring();
}
base.ItemAdding(properties);
}
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
}
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
}
private static int GetUniqueId(SPWeb web)
{
int returnValue = -1;
int retryCounter = 0;
int uniqueId = -1;
EventLog LogToSystem = new EventLog();
ReTry:
try
{
//Set the source of the Errors that are logged to the Event
log.
LogToSystem.Source = "CreateDocID.GetUniqueId";
using (SPSite sitecol = web.Site)
{
//get the counter list in the root site collection
SPList counterList = sitecol.RootWeb.Lists["Document
Counter"];
//get the list item
SPListItem counterItem = counterList.Items[0];
// Get the value for the unique id
uniqueId = int.Parse(counterItem["Title"].ToString());
// Increment and update the value
counterItem["Title"] = uniqueId + 1;
counterItem.Update();
// Set the value to return
returnValue = uniqueId;
}
}
catch (Exception ex)
{
// Handle the exception by retrying 5 times
retryCounter += 1;
if (retryCounter <= 5)
{
System.Threading.Thread.Sleep(2500);
goto ReTry;
}
else
{
// Write the exception to the log
LogToSystem.WriteEntry(ex.StackTrace,
EventLogEntryType.Error);
}
}
return returnValue;
}
private static string GetUniqueDateId()
{
string returnValue = "-1";
string retryCounter = "0";
string sUniqueID = "-1";
EventLog LogToSystem = new EventLog();
ReTry:
try
{
//Set the source of the Errors that are logged to the Event
log.
LogToSystem.Source = "CreateDocID.GetUniqueDateId";
// Get the value for the unique id
sUniqueID = string.Format("{0:ddMMyy}", DateTime.Now);
// Set the value to return
returnValue = sUniqueID;
}
catch (Exception ex)
{
LogToSystem.WriteEntry(ex.StackTrace,
EventLogEntryType.Error);
}
return returnValue;
}
}
}
On Fri, Feb 12, 2010 at 12:13 PM, Paul Turner <[email protected]>wrote:
> Any Code?? Event Receivers are very specific and need certain methods.
>
>
>
> * *
>
> *Regards,*
>
> * *
>
> *Paul Turner
> *Senior Solutions Specialist
>
> *M: *0412 748 168 *P: *08 8238 0912 *F:* 08 8234 5966
> *A:* 66 Henley Beach Road, Mile End SA 5031
> *E:* *[email protected]* *W:* www.dws.com.au
>
> [image: dws logo]
>
> ADVANCED BUSINESS SOLUTIONS LTD
>
>
>
> This email and any files transmitted with it are confidential and are only
> for the use of the person to whom they are addressed. If you are not the
> intended recipient you have received this email in error and are requested
> to delete it immediately. Any opinion expressed in this e-mail may not
> necessarily be that of DWS Pty Ltd.
> *Please consider the environment before printing this email.*
>
>
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *Joe Breen
> *Sent:* Friday, 12 February 2010 10:17 PM
> *To:* [email protected]
> *Subject:* Event Handler Not Firing
>
>
>
> Hello.
>
>
>
> I have created an Event Receiver and bound it to a document library
> (ListTemplateId = 101). I have checked that it is bound correctly with this
> utility: http://spstipsutilitypack.codeplex.com/releases/view/39102
>
>
>
> Everything seems to be ok and it seems to be bound correctly to the
> document library. I have tried redeploying the Feature and also
> deactivating and reactivating it.
>
>
>
> When I add a new document either through saving or through upload to the
> document library neither the ItemAdded or ItemAdding events get fired.
>
>
>
> I have tried deactivating and reactivating the Feature.
>
>
>
> Below is the Feature.xml and the Elements.xml files:
>
>
>
> Feature.xml *******
>
> <?xml version="1.0" encoding="utf-8" ?>
>
> <Feature Id="112F06E5-A4DA-4ebc-A371-9AC661FF23B5"
>
> Title="Create Document ID"
>
> Description="Feature that generates a Document ID"
>
> Version="12.0.0.0"
>
> Hidden="FALSE"
>
> Scope="Web"
>
> DefaultResourceFile="core"
>
> xmlns="http://schemas.microsoft.com/sharepoint/">
>
> <ElementManifests>
>
> <ElementManifest Location="CreateDocumentID.xml"/>
>
> </ElementManifests>
>
> </Feature>
>
>
>
> *********
>
>
>
> CreateDocumentID.xml *******
>
>
>
> <?xml version="1.0" encoding="utf-8" ?>
>
> <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
>
> <Receivers ListTemplateId="101">
>
> <Receiver>
>
> <Name>CreateDocIDItemAdded</Name>
>
> <Type>ItemAdded</Type>
>
> <SequenceNumber>10000</SequenceNumber>
>
> <Assembly>CreateDocID, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=036b52b8bf77e9e8</Assembly>
>
> <Class>CreateDocID.CreateDocumentID</Class>
>
> <Data></Data>
>
> <Filter></Filter>
>
> </Receiver>
>
> <Receiver>
>
> <Name>CreateDocIDItemAdding</Name>
>
> <Type>ItemAdding</Type>
>
> <SequenceNumber>10000</SequenceNumber>
>
> <Assembly>CreateDocID, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=036b52b8bf77e9e8</Assembly>
>
> <Class>CreateDocID.CreateDocumentID</Class>
>
> <Data></Data>
>
> <Filter></Filter>
>
> </Receiver>
>
> </Receivers>
>
> </Elements>
>
>
>
> *********
>
>
>
> What am I doing wrong?
>
> _______________________________________________
> ozmoss mailing list
> [email protected]
> http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
>
>
<<image001.jpg>>
_______________________________________________ ozmoss mailing list [email protected] http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
