現在のドキュメント ライブラリにフォルダーを追加するにはどうすればよいですか (フォルダーが現在のドキュメント ライブラリに存在しない場合)。
(現在はエンド ユーザーがどこにいても) (このコードを itemAdded イベント ハンドラーに追加します)
現在のドキュメント ライブラリにフォルダーを追加するにはどうすればよいですか (フォルダーが現在のドキュメント ライブラリに存在しない場合)。
(現在はエンド ユーザーがどこにいても) (このコードを itemAdded イベント ハンドラーに追加します)
curSPList.Items.Add("My Folder Name", SPFileSystemObjectType.Folder);
ドキュメント ライブラリを含む任意の SharePoint リストに新しいフォルダーが作成されます。これをイベント ハンドラーに実装する場合は、SPItemEventProperties パラメーターの "List" プロパティから SPList への参照を取得できます。
これが機能する最終的なコードです。「uHippo」が存在しない場合、現在のドキュメント ライブラリにフォルダー「uHippo」を作成します。
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
using (SPSite currentSite = new SPSite(properties.WebUrl))
using (SPWeb currentWeb = currentSite.OpenWeb())
{ SPListItem oItem = properties.ListItem;
string doclibname = "Not a doclib";
//Gets the name of the document library
SPList doclibList = oItem.ParentList;
if (null != doclibList)
{
doclibname = doclibList.Title;
}
bool foundFolder = false; //Assume it isn't there by default
if (doclibList.Folders.Count > 0) //If the folder list is empty, then the folder definitely doesn't exist.
{
foreach (SPListItem fItem in doclibList.Folders)
{
if (fItem.Title.Equals("uHippo"))
{
foundFolder = true; //Folder does exist, break loop.
break;
}
}
}
if (foundFolder == false)
{
SPListItem folder = doclibList.Folders.Add(doclibList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "uHippo");
folder.Update();
}
}
}