如何在sharepoint 2010中动态创建文档库中的文件夹 [英] How to create folders in document library dynamically in sharepoint 2010

查看:132
本文介绍了如何在sharepoint 2010中动态创建文档库中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在同一个库中创建任何新文件夹时在文档库中创建动态文件夹和子文件夹。

此外,我想对可能出现在另一个子网站中的文档库的数量重复相同。



无论何时添加任何用户,我都会创建一个示例代码来创建文件夹任何新文件夹。它正在创建文件夹,但在调试时它会抛出数组越界错误。虽然它按照需要创建文件夹。



公共类DynamicFolderCreationEventReciever:SPItemEventReceiver 
{
private string [] folders = new string [] {One , 二三 };
///< summary>
///添加了一个项目。
///< / summary>
public override void ItemAdded(SPItemEventProperties properties)
{
try
{
using(SPSite objSPSiteCollection = new SPSite(SPContext.Current.Web.Url))
{
using(SPWeb objSPWeb = objSPSiteCollection.RootWeb)
{

// base.ItemAdded(properties);
SPListItem item = properties.ListItem;
// SPDocumentLibrary objDocLib =(SPDocumentLibrary)objSPWeb.Lists [properties.ListId];
SPDocumentLibrary obj = objSPWeb.Lists [CustomDocLibrary] as SPDocumentLibrary;
if(item.FileSystemObjectType == SPFileSystemObjectType.Folder)
{
String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
SPFolder libFolder = obj.RootFolder.SubFolders [properties.ListItem.Name];

string newFolderUrl =(objSPWeb.Url +/+ libFolder.ToString());

for(int i = 0; i< folders.Length; i ++)
{
SPListItem newFolder = obj.Items.Add(newFolderUrl,SPFileSystemObjectType.Folder,folders [ I]的ToString());
newFolder.Update();
}
}
其他
{

}


}
}
}
catch(SPException ex)
{
throw ex;
}
}

}

 





我不知道调试时出现错误的原因。我怎样才能创建子文件夹。







谢谢

解决方案

Hi,

I want to create dynamic folders and subfolders in a document library when any new folder created in the same library.
Also I want to repeat the same for number of document libraries which may present in another subsites.

I have a made a sample code to create folder whenever any user add any new folder. It is creating the folders but at the time of debugging it is throwing array out of bounds error. Though it is creating folders as per desired.

public class DynamicFolderCreationEventReciever : SPItemEventReceiver
    {
        private string[] folders = new string[] { "One", "Two", "Three" };
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           try
           {
               using (SPSite objSPSiteCollection = new SPSite(SPContext.Current.Web.Url))
               {
                   using (SPWeb objSPWeb = objSPSiteCollection.RootWeb)
                   {

                      // base.ItemAdded(properties);
                       SPListItem item = properties.ListItem;
                       //SPDocumentLibrary objDocLib = (SPDocumentLibrary)objSPWeb.Lists[properties.ListId];   
                       SPDocumentLibrary obj=objSPWeb.Lists["CustomDocLibrary"] as SPDocumentLibrary;
                       if (item.FileSystemObjectType== SPFileSystemObjectType.Folder )
                       {
                           String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
                           SPFolder libFolder = obj.RootFolder.SubFolders[properties.ListItem.Name];

                           string newFolderUrl = (objSPWeb.Url + "/" + libFolder.ToString());

                           for (int i = 0; i < folders.Length; i++)
                           {
                               SPListItem newFolder = obj.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, folders[i].ToString());
                               newFolder.Update();
                           }
                       }
                       else
                       {

                       }


                   }
               }
           }
           catch (SPException ex)
           {
               throw ex;
           }
       }

    }



I don't know why that error coming while debugging. And how can I create subfolders.



Thanks

解决方案

这篇关于如何在sharepoint 2010中动态创建文档库中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆