Solved

folder creation in sub folder of "Shared Document" library

Posted on 2010-09-13
3
929 Views
Last Modified: 2012-05-10
Hi, I have following code where I am able to create folder in "Shared Document" library when item is added in list but i want that folder should get created in already exit folder like I have already "Test " folder in library , in this folder should get created sub folders of title of list item added in list which getting created in root level of library,

please check my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using System.Configuration;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;
namespace ABC_EventHandler
{
    public class CreateFolder : Microsoft.SharePoint.SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                this.DisableEventFiring();
                string sUrlOfWikiPage = string.Empty;
                string sUrlOfNewFolder = string.Empty;
                string sSubsiteRUL = string.Empty;
                string sCurrentItemTitle = properties.ListItem["Title"].ToString();
                string sDocLibName = "Shared Documents";
                string sTRListID = "ABC Status";
                this.EnableEventFiring();

//Since the event handler is attached based on base ID of a list, this event handler will be attached to all custom lists
//To prevent it from being executed on other custom lists (which will create an error), the following validation is done.

                if (sTRListID.ToUpper().Equals(properties.ListTitle.ToString().ToUpper()))
                {
                    //Create the Folder
                    sUrlOfNewFolder = CreateFolder(properties.ListItem.Web, sDocLibName, sCurrentItemTitle);

                                       
                   
                    properties.ListItem.Update();
                    properties.ListItem.UpdateOverwriteVersion();
                  
                }
            }
            catch(Exception excGeneral)
            {
               LogError(properties.ListItem.Web, "ItemAdded -->" + excGeneral.Message + "-->" + excGeneral.InnerException.ToString() + "-->" + excGeneral.StackTrace);
            }
        }
         public string CreateFolder(SPWeb oWeb, string sDocLibName, string sTitle)
        {
            try
            {
                SPDocumentLibrary docLib = (SPDocumentLibrary)oWeb.Lists[sDocLibName];
                SPFolder DLrootFolder = docLib.RootFolder;
                SPFolder oNewFolder = DLrootFolder.SubFolders.Add(oWeb.Url + "/" + sDocLibName + "/" + sTitle);
                return oWeb.Url + "/" + oNewFolder.Url.ToString();
            }
            catch (Exception excCreateFolder)
            {
                LogError(oWeb, "CreateFolder -->" + excCreateFolder.Message + "-->" + excCreateFolder.InnerException.ToString() + "-->" + excCreateFolder.StackTrace);
                throw excCreateFolder;
            }
        }

        /// <summary>
        /// This method logs any error(s) that happens within the event handler into a list
        /// Any exception that happens within this method is not handled and throw to the system to handle.
        /// </summary>
        /// <param name="oWeb">The SPWeb object which holds the "Errors List"</param>
        /// <param name="sErrorMessage">The detailed error</param>
        public void LogError(SPWeb oWeb,  string sErrorMessage)
        {
            try
            {
                string sErrorsListName = ConfigurationSettings.AppSettings["ErrorsList"].ToString();
                SPList oErrorsList = oWeb.Lists[sErrorsListName];
                SPListItem oNewItem = oErrorsList.Items.Add();
                oNewItem["Title"] = sErrorMessage;
                oNewItem.Update();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        }


        }

Open in new window

0
Comment
Question by:sandipkharde
  • 2
3 Comments
 
LVL 7

Expert Comment

by:EagleUK
Comment Utility
A little more information helps people help you solve your problem. What symptoms are you experiencing? When do the errors occur?
0
 
LVL 8

Accepted Solution

by:
sandipkharde earned 0 total points
Comment Utility
solved issue by playing with code....there was issue with concatenation of wording when item is added in lis.
0
 
LVL 8

Author Closing Comment

by:sandipkharde
Comment Utility
.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now