All I want to do is to create a folder "MetaFolder" inside a document library.
User can be on any document library and I would like to create this folder after item is added (so on itemAdded event handler).
I do NOT want workflow so please dont suggest workflow. This code works but I have to hardcode the url but need to get url from current url. also need to verify the folder uHippo does not exists in the current doc library...
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
using (SPSite currentSite = new SPSite(properties.WebUrl))
using (SPWeb currentWeb = currentSite.OpenWeb())
{ // This code works and creates Folder in the "My TEST Doc library"
//SPList docLib = currentWeb.Lists["My TEST Doc Library"];
//SPListItem folder = docLib.Folders.Add(docLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "My folder");
//folder.Update();
string doclibname = "Not a doclib";
//SPList doclibList = currentWeb.GetList(HttpContext.Current.Request.RawUrl); // NOT WORKING. Tried properties.weburl
SPList doclibList = currentWeb.GetListFromUrl("https://mycompanyportal/sites/testsitecol/testwebsite/My%20TEST%20Doc%20Library/Forms/AllItems.aspx");
if (null != doclibList)
{
doclibname = doclibList.Title;
}
// this section also not working.
// getting Object reference not set to an instance of an object or something like that.
//if (currentWeb.GetFolder("uHippo").Exists == false)
//{
SPListItem folder = doclibList.Folders.Add(doclibList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "uHippo");
folder.Update();
//}
}
}