Adding folders and files to Sharepoint document libraries

I am working on project now where we are creating reports and saving them to a document library in Sharepoint 3. I am automating this by using .NET code to create the subdirectories and files in the document library. I have not seen a lot of good code examples on how to do this on the internet, but this is how I am doing it in C#.

When I add a folder to the document library I used the following code;

SPSite mySite = new SPSite("http://sitename/");
SPWeb myWeb = mySite.AllWebs["mywebname"];

SPFolderCollection myFolderCollection = myWeb.GetFolder("http://sitename/sites/mywebname/PDF Reports").SubFolders;
myFolderCollection.Add("FolderName");

To add a file from a filestream, I used the following code;

FileStream stream = File.Create("report.pdf", result.Length);

SPSite mySite = new SPSite("http://sitename/");
SPWeb myWeb = mySite.AllWebs["mywebname"];

SPFileCollection destFiles = myWeb.GetFolder("http://sitename/sites/mywebname/PDF Reports/FolderName").Files;
destFiles.Add("report.pdf", stream, true);

stream.Close();

Comments
Umesh's Gravatar I am creating a new sub-site programmatically and want to add folders
and files programmatically. I can create folders but can't create new
aspx pages into that. This is not a publishing site. How I can do that?
# Posted By Umesh | 1/20/08 11:45 PM
David Fekke's Gravatar Umesh,

What message do you get back when you try to add files to the folder? Can you add static content to the older such as Word Docs? I have never had to add an aspx page to a folder other than as a feature. I would like to hear more about this problem.

Welcome to the wonderful world of Sharepoint development.
David
# Posted By David Fekke | 1/21/08 6:00 AM
Michele's Gravatar Hi, thank you for dharing your code!
Is really useful, but i-ll ask a step further... if i would like to add additional information to that file (example> search parameters or specific comments) do you have encountered such a problem?
Thank you in advance for your effort
# Posted By Michele | 1/28/08 3:54 AM
David Fekke's Gravatar You can create a hashtable object and add it in to the add method after the stream or byte[] parameter. You can use the hashtable to pass in additional parameters into the document library item.
# Posted By David Fekke | 1/28/08 5:34 AM
Michele's Gravatar Thanks a lot...
where i can find docs or exaples to store that informations on hash table in the DB of Share Point_
Thank in advance
# Posted By Michele | 1/28/08 7:59 AM
David Fekke's Gravatar Unfortuneatly, Sharepoint 2007 is not well documented. I would just try to google and read the blogs to see where people have had success.

Thank you,
David.
# Posted By David Fekke | 1/29/08 9:59 AM
Humberto's Gravatar Hi David I had a problem whe i run your code to create a new folder,
i get an SPException of security validation. Do you know why i get this exception
sorry if my english is not so good ok.

Thanks in advance
# Posted By Humberto | 5/20/08 3:08 PM
David Fekke's Gravatar Hola Humberto,

The problem you are having is a permissions related problem. Make the the user logged in has permissions to add files to that folder.

Good luck,
David.
# Posted By David Fekke | 5/20/08 5:02 PM
Humberto's Gravatar Hi again David

this is my code. Do you think that i put the pathfolder wrong ?? Or How cai i do to authenticate my account. I ran the code as an administrator and it has privileges on sharepoint server too. Whe I run a webservice i can do this.


wsSubeArchivo.PreAuthenticate = true;
wsSubeArchivo.Credentials = new System.Net.NetworkCredential("user", "pwd", "domain");

How can i do the same in your code to authenticate first my user.

Thanks in advance





SPSite mySite = new SPSite("http://sener_dev_001:23456/DUIT/Documentos%20compartidos/Forms/AllItems.aspx");
SPWeb myWeb = mySite.AllWebs["DUIT"];

SPFolderCollection myFolderCollection = myWeb.GetFolder("%2fDUIT%2fDocumentos%20compartidos").SubFolders;
myFolderCollection.Add("http://sener_dev_001:23456/DUIT/Documentos%20compartidos/Forms/AllItems.aspx?RootFolder=%2fDUIT%2fDocumentos%20compartidos%2fSAI");
# Posted By Humberto | 5/20/08 6:44 PM
Humberto's Gravatar Hi David,
I've a problem developing a process to create and insert files from a folder into
a site document library.
I ran my code as an administrator on my developmetn server and
SharePoint, server because is the same.

When i try to execute your code an error appears:
> COMException (0x8102006d): La validación de seguridad de esta página no es
> correcta. Haga clic en Atrás en el explorador Web, actualice la página y
> vuelva a intentar la operación.]
> Microsoft.SharePoint.Library.SPRequestInternalClass.PutFile(String
> bstrUrl, String bstrWebRelativeUrl, Object varFile, PutFileOpt PutFileOpt,
> String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32
> iModifiedByID, Object varTimeCreated, Object varTimeLastModified) +0
> Microsoft.SharePoint.Library.a.a(String A_0, String A_1, Object A_2,
> PutFileOpt A_3, String A_4, String A_5, Int32 A_6, Int32 A_7, Object A_8,
> Object A_9)
>
> [SPException: La validación de seguridad de esta página no es correcta. Haga
> clic en Atrás en el explorador Web, actualice la página y vuelva a intentar
> la operación.]
> Microsoft.SharePoint.Library.a.a(String A_0, String A_1, Object A_2,
> PutFileOpt A_3, String A_4, String A_5, Int32 A_6, Int32 A_7, Object A_8,
> Object A_9)
> Microsoft.SharePoint.SPFileCollection.a(String A_0, Byte[] A_1,
> PutFileOpt A_2, String A_3, String A_4, Int32 A_5, Int32 A_6, DateTime A_7,
> DateTime A_8)
> Microsoft.SharePoint.SPFileCollection.Add(String strUrl, Byte[] file,
> Boolean overwrite)
> Microsoft.SharePoint.SPFileCollection.Add(String strUrl, Byte[] file)
> miAplicacion.CargarFicheros.UploadFile(String srcUrl, String destUrl) in
> f:\archivos de programa\archivos comunes\microsoft shared\web server
> extensions\60\template\layouts\miaplicacion\cargarficheros.aspx.cs:138
> miAplicacion.CargarFicheros.Button1_Click(Object sender, EventArgs e) in
> f:\archivos de programa\archivos comunes\microsoft shared\web server
> extensions\60\template\layouts\miaplicacion\cargarficheros.aspx.cs:151
> System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
>
> System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
> System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
> sourceControl, String eventArgument) +18
> System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
> System.Web.UI.Page.ProcessRequestMain() +1292




> What is the problem? Could you help me? i tried to authenticate my user
as following

NetworkCredential nc = new NetworkCredential("user","password","domain");
WebClient wc = new WebClient();
wc.Credentials = nc;

Another try

SPRoleDefinition roldef = myWeb.RoleDefinitions.GetByType(SPRoleType.Administrator);
SPRoleAssignment rolassign = new SPRoleAssignment("domain\\user","","","");
rolassign.RoleDefinitionBindings.Add(roldef);


But anything works when i run the code the SPException appears again.

How do you run your code. Suppose that i tried to run your code on a webapplications and webservice too but the error appears on both codes.

Thanks for advance.
# Posted By Humberto | 5/21/08 3:11 PM
Andras's Gravatar Hi!

I have the following problem when uploading files to Sharepoint Portal 2007:
There are 13 overloads of the SPFileCollection.ADD, but none allow to preserve all aspects of the version:
- One overload allows setting of Modified and ModifiedBy - but not Version comments
- One overload allows setting version comment but not Modified/ModifiedBy.
- One allows sending the version's property bag over, but this seems to have no effect on version comments or Modified/ModifedBy on the destination file.
If I upload a document by setting Modified and ModifiedBy and change the comment afterwards, the Last modified by property of the document is changed to the user running the code.
Could somebody please help? Thanks in advance!
# Posted By Andras | 6/12/08 8:58 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.6.001.