Wednesday 5 March 2014

Folder Creation in SharePoint 2010 Document Library

I have used the following code for folder creation only in document library via list item creation workflow
SPListItem CurItem = workflowProperties.Item;
SPSite MySite = new SPSite(workflowProperties.WebUrl);
SPWeb Myweb = MySite.OpenWeb();

SPDocumentLibrary spdoclibrary = (SPDocumentLibrary)Myweb.Lists["My Documents"];
Myweb.Folders.Add(workflowProperties.WebUrl + "/My%20Documents/" + CurItem["Title"].ToString());

spdoclibrary.Update();  

Another Way:-
SPList splstDoc = Myweb.Lists["My Documents"];
SPFolderCollection folderColl = splstDoc.RootFolder.SubFolders;
SPFolder newFolder = folderColl.Add(workflowProperties.WebUrl + "/My%20Documents/" + CurItem["Title"].ToString());        

I have used the following code for file along with properties creation in document library via list item creation workflow

Hashtable objhash = new Hashtable();
byte[] byt = new byte[Convert.ToInt32(FileUpload1.PostedFile.ContentLength)];
objhash.Add("custom field name", "value");
SPFolder myfolder = Myweb.GetList(workflowProperties.WebUrl + "/My%20Documents/").RootFolder;
myfolder.Files.Add("Test.txt", byt, objhash, false);           
myfolder.Update();


I have used the following code for folder along with properties creation in document library via list item creation workflow

SPFolder myfolder = Myweb.GetFolder(workflowProperties.WebUrl + "/My%20Documents/");
SPFolder spfolder = myfolder.SubFolders.Add(workflowProperties.WebUrl + "/My%20Documents/" + CurItem["Title"].ToString());
spfolder.Item["Title"] = "Aasai";
spfolder.Item["Test"] = "Aasai";
spfolder.Item.SystemUpdate(false);