Here is a way how you can add your local files to SPFolder. All you need to do is just read the local file as a streams and then just called Files.Add Method to add a file in the Folder.
SPWeb objWeb = SPContext.Current.Web;
objWeb.AllowUnsafeUpdates = true;
SPFolderCollection objFolderColl = objWeb.Folders;
foreach (SPFolder objFolder in objFolderColl)
{
if (objFolder.Name == "Groups Document")
{
FileStream fStream = File.OpenRead("C:\\groups.txt");
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
objFolder.Files.Add("Testdoc.doc", contents);
objFolder.Update();
break;
}
}
objWeb.AllowUnsafeUpdates = false;
That's it. Your job is done.
2 comments:
Hi SPKings
Your solution is a little incomplete.
When considering one might be able to add specific metadata (such as a specific content type) before adding the item to ensure property values exists when events (e.g. ItemAdded) are fired.
More complete might be:
Hashtable hashTable = new Hashtable(); hashTable.Add("ContentTypeId", -ContentTypeId-);
hashTableProperties.Add("Title", -Title-);
objFolder.Files.Add("Testdoc.doc", contents, hashTable);
In the above example, all the corresponding content type specific events will fire on item creation (which was the solution i was looking for).
Thanks for the informative blog!
Hi arnhem,
thanks to you for sharing your thought and solution.
Post a Comment