I have some code attached to a list which on adding an item to a list will go off and create a document library for that item.
code for this is below:
I install this event handler to the library with a c# application code below:
This all works great and has been for some time, however, recently I wanted to then add some more function to this and wanted to (once created) add some folders into the document library which was created when the item is added.
so I wrote the following code:
So straight away I thought great, what I will do is add this code after the doc library creation code in the first block, letting it run after the library is created and event firing is re-enabled... Still couldnt get it to work.
Now the weird bit, if I simply paste the code in at the end of my application to install my event handler I can get it to create a folder in any library. however this code is only run the once to install the event handler.
Could anybody point me in the right direction? The more I look at it the more confused I am getting, why cant I run the 3rd block of code after the event firing is re-enabled at the end of my event handler.
Cheers guys.
code for this is below:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ListLibEventHandlerNew
{
public class ListLibEventHandlerClass : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
try
{
base.DisableEventFiring();
string listTitle = properties.AfterProperties["Title"].ToString();
using (SPWeb web = properties.OpenWeb())
{//add doc lib
Guid newListID = web.Lists.Add(listTitle, "", SPListTemplateType.DocumentLibrary);
//additional properties
SPList newList = web.Lists[newListID];
newList.OnQuickLaunch = false;
newList.EnableVersioning = true;
newList.EnableMinorVersions = true;
newList.ForceCheckout = true;
newList.Update();
}
}
catch (Exception exception)
{
}
finally
{
base.EnableEventFiring();
}
}
}
}
I install this event handler to the library with a c# application code below:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ListLinRegApp
{
class Program
{
static void Main(string[] args)
{
SPSite sp = new SPSite("URL HERE");
SPWeb website = sp.OpenWeb();
SPList ListLib = website.Lists["CLIST NAME HERE"];
//key token needs updating from the token in GAC.
String custassembley = "ListLibEventHandlerNew, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c861ccb329f5f087";
String classname = "ListLibEventHandlerNew.ListLibEventHandlerClass";
ListLib.EventReceivers.Add(SPEventReceiverType.ItemAdded, custassembley, classname);
}
}
}
This all works great and has been for some time, however, recently I wanted to then add some more function to this and wanted to (once created) add some folders into the document library which was created when the item is added.
so I wrote the following code:
Code:
SPSite _MySite = new SPSite("HOME URL");
SPWeb _MyWeb = _MySite.OpenWeb();
SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)_MyWeb.Lists["DOC LIB NAME"];
SPFolderCollection _MyFolders = _MyWeb.Folders;
_MyFolders.Add("HOME URL/docs/" + "test" + "/");
_MyDocLibrary.Update();
So straight away I thought great, what I will do is add this code after the doc library creation code in the first block, letting it run after the library is created and event firing is re-enabled... Still couldnt get it to work.
Now the weird bit, if I simply paste the code in at the end of my application to install my event handler I can get it to create a folder in any library. however this code is only run the once to install the event handler.
Could anybody point me in the right direction? The more I look at it the more confused I am getting, why cant I run the 3rd block of code after the event firing is re-enabled at the end of my event handler.
Cheers guys.
Last edited: