Create a folder in a document library

Jan 4, 2010 at 10:44 AM

Hello,

I would like to know if it does possible to create a folder in a document library ?

Thanks in advance !! :-)

Coordinator
Jan 4, 2010 at 1:26 PM

Yes, of course! You can use the UpdateListItems operation of the Lists Web Service.  If you look at the SDK for that operation (http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx), you'll see an example for the updates parameter to create a folder:

<Batch OnError="Continue" PreCalc="TRUE" 
ListVersion="0" 
ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
   <Method ID="1" Cmd="New">
      <Field Name="ID">New</Field>
      <Field Name="FSObjType">1</Field>
      <Field Name="BaseName">Name</Field>
   </Method>
</Batch>

Let me know if you have more questions...

M.

Jan 4, 2010 at 1:44 PM

I code this script :

<script language="javascript" type="text/javascript" src="/HRD/JQuery/jquery-1.3.2.min.js"></script>
<script language="javascript" type="text/javascript" src="/HRD/JQuery/spservices/jquery.SPServices-0.4.7.min.js"></script>

<script language="javascript" type="text/javascript" >
function PreSaveAction() {
    var folderName = "essai";
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        listName: "documents",
        updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
                "<Method ID='1' Cmd='New'>" +
                    "<Field Name='FSObjType'>1</Field>" +
                    "<Field Name='BaseName'>" + folderName + "</Field>" +
                "</Method>" +
            "</Batch>",
        completefunc: function(xData, Status) {
            …
        }
    });
    return true;
}</script>

Where listName is my "Document Library" and "folderName", the folder that I want to create.

But my folder is not created...

The "PreSaveAction" is correctly executed.

 

Coordinator
Jan 4, 2010 at 1:53 PM

I think you need to be more explicit with the folderName, perhaps giving the full path.  Add an alert in the completefunc to see what the function is returning.  (The error messages aren't usually all that helpful, but sometimes they give you a hint.)

alert("Status=" + Status + " XML=" + xData.responseXML.xml);

M.

Jan 4, 2010 at 2:42 PM

Thanks a lot !!

That's run !!

I use a GUID for identify my list :

<script language="javascript" type="text/javascript" >
function PreSaveAction() {
	var folderName = "essai";
	$().SPServices({
		operation: "UpdateListItems",
		async: false,
		listName: "{E92E9BE1-ECC9-4529-BA1C-81C7D1FFCA95}",
		updates: "<Batch OnError='Continue' PreCalc='TRUE' ListVersion='0' >" +
				"<Method ID='1' Cmd='New'>" +
					"<Field Name='FSObjType'>1</Field>" +
					"<Field Name='BaseName'>" + folderName + "</Field>" +
				"</Method>" +
			"</Batch>",
		completefunc: function(xData, Status) {
			alert("Status=" + Status + " XML=" + xData.responseXML.xml);		}
	});
	return true;
}</script>

 

 

Coordinator
Jan 4, 2010 at 2:53 PM

Glad you got it working! The ListName can contain either the actual name or the GUID, though the GUID can be more reliable since it doesn't change.

I just put your example into the UpdateListItems documentation so that others can benefit.

M.

Feb 9, 2011 at 12:01 PM

 

Hi,

It is really useful. But I am trying to retrieve all the folder names which I created in a document library. I am using the below code, but didn't work :(

camlFolder = "<Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>folder</Value></Eq></Where></Query>";

 $().SPServices({
     operation:  "GetListItems",
        listName: "Document Lib"

        webURL: "/sites/Mysite",
         CAMLQuery: camlFolder,
       
     completefunc: function (xData, Status) {
                                 alert('Folder Name is'+foldername);

}

});
         
Can anyone help me please ?    
         

Coordinator
Feb 10, 2011 at 11:13 PM

It works a little differently than that. Try this:

$().SPServices({
  operation:  "GetListItems",
  listName: "jQuery Libraries",
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
      var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1];
      if(thisFSObjType == 1) alert($(this).attr("ows_Title") + "::" + $(this).attr("ows_FSObjType"));
    });
  }
});

You were also missing a comma at the end of the listName: "Document Lib" line.

M.

Jun 8, 2011 at 9:18 AM

Thanks for replying Marc. This helps !

But how to access the items present inside folders ? I have say two/three items present inside a folder in document library. How can I list the items present inside every folder ?

Thanks

 

Coordinator
Jun 8, 2011 at 1:13 PM

Well, you asked how to view folders, so I gave you that code above. To get the items, you just use GetListItems in the more "normal" way (wthout the if(thisFSObjType == 1) filter).

M.

Jun 9, 2011 at 8:40 AM

I have modified my code like this :

var queryOpt = "<QueryOptions><ViewAttributes Scope='RecursiveAll' /></QueryOptions>";
var viewFields = "<ViewFields><FieldRef Name='Title' /></ViewFields>";

    $().SPServices({
      operation:  "GetListItems",
      listName: "MyDoc Lib",
      CAMLViewFields: viewFields,
      CAMLQueryOptions: queryOpt,
      completefunc: function (xData, Status) {
        $("#divId").append("<ul>");
        $(xData.responseXML).find("[nodeName='z:row']").each(function() {
                 var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1];
          if(thisFSObjType == 0) {
            $("#divId").append("<li>" + thisFSObjType +
            $(this).attr("ows_FileLeafRef").split(";#")[1] +
              "  [" +
                $(this).attr("ows_FileLeafRef") +
              "]" +
            "</li>");
          }
        });

        $("#divId").append("</ul>");
      }
    });

 

Now this is listing the ONLY the files present in the document library, as I am checking for thisFSObjType=1. But can anyone help me to list them according to the folders. Like

Folder A

- Doc 1

- Doc 2

Folder B

- Doc 1

- Doc 2

 

Thanks

Coordinator
Jun 13, 2011 at 1:57 PM

Your code doesn't seem to match what you say you're doing. You really need to read the MSDN SDK, which I provide links to in the SPServices docs.

If you specify

CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='Recursive' /></QueryOptions>",

in the GetListItems call, then you will get all of the list items back, including those within folders. You can then format the results based on the contents of the ows_FileRef attribute of each list item.

M.

Apr 19, 2012 at 5:32 PM

Hi,

I want to group my list items in folders.

My list will contain a lot of items when released into production environment so I thought to group them into folders named using the year and month of the item's creation, the folders names of would look like "201201" for all items created in January 2012 and so on.

Assuming all this makes sense, my question is: can this be done by tweaking Marc's code (posted at the beginning of this discussion)? or do I need to implemented in a different way?.

Thank you in advance.

Coordinator
Apr 20, 2012 at 12:50 AM
Edited Apr 20, 2012 at 12:50 AM

Fabian:

Take a look at this old blog post of mine. I think I was doing something similar to what you describe.
http://sympmarc.com/2009/12/08/a-jquery-library-for-sharepoint-web-services-wss-3-0-and-moss-real-world-example-part-1/

M.

Apr 20, 2012 at 7:13 PM
Edited Apr 23, 2012 at 8:02 PM

Hi Marc,

After reading the blog post I changed my mind, I will not use year and month as name for the folders. 

I tested the code to create the folders and works very nicely, I just can't get the item to be created inside the new folder. I saw other threads and now I know I have to use the BaseName field.

I'm guessing I have to hide the Ok and Cancel buttons and put them of my own to implement the save functionality, did you do something like this for that project you sent me the link?

Fabian.

Jun 4, 2012 at 3:11 PM

I know this post is a bit dated but I found an extra piece of information about creating folders that I thought should be documented on the site. I was having trouble with the view sorting properly after creating the folder in a view where I have this view set to show 'In the top-level folder'.   The newly created folder did not fall in to the proper sort order until I edited and saved.   I found if I add the Title field and set that value as well the sorting started working....

updates: "<Batch OnError='Return' PreCalc='TRUE' ListVersion='0'>" +
                "<Method ID='1' Cmd='New'>" +
            "<Field Name='ID'>New</Field>" +
                    "<Field Name='FSObjType'>1</Field>" +
                    "<Field Name='BaseName'>" + folderName + "</Field>" +
                    "<Field Name='Title'>" + folderName + "</Field>" +
                "</Method>" +
            "</Batch>",

Jul 12, 2012 at 9:19 AM

i was trying to create floder sharepoint 2010 with below code.this coding working  if Document Library does not contains any Content Type.But its not working when Library associted with anothe contect type and Content type contains Managed meta Data field.please suggest me any one how can modify this code to make it work when library having managed Data Filed.
var folderName = "essai";
$().SPServices({
operation: "UpdateListItems",
async: false,
listName: "{E92E9BE1-ECC9-4529-BA1C-81C7D1FFCA95}",
updates: "<Batch OnError='Continue' PreCalc='TRUE' ListVersion='0' >" +
"<Method ID='1' Cmd='New'>" +
"<Field Name='FSObjType'>1</Field>" +
"<Field Name='BaseName'>" + folderName + "</Field>" +
"</Method>" +
"</Batch>",
completefunc: function(xData, Status) {
alert("Status=" + Status + " XML=" + xData.responseXML.xml); }
});
return true;
}</script>

Coordinator
Jul 12, 2012 at 2:29 PM

veereshinduvasi:

What are you getting in the XML response? Does it indicate anything about the error?

M.

Jul 13, 2012 at 6:55 AM

http://www.w3.org/2001/XMLSchema"><soap:Body><UpdateListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><UpdateListItemsResult><Results><Result ID="1,New"><ErrorCode>0x81020014</ErrorCode><ErrorText>One or more field types are not installed properly. Go to the list settings page to delete these fields.</ErrorText></Result></Results></UpdateListItemsResult></UpdateListItemsResponse></soap:Body></soap:Envelope>

when i create folder from UI and execute get list item the folder XML as below

 it will returen XML as below:

<z:row ows_LinkFilename="folder1" ows_Modified="2012-07-11 08:21:09" ows_Editor="53;#Induvasi, Veeralaxminarayana SOMLOG-BNGLOG" ows__ModerationStatus="0" ows__Level="1" ows_ID="8" ows_UniqueId="8;#{AE91EB97-FDDD-47C0-83F8-5AFFFDF966B0}" ows_owshiddenversion="1" ows_FSObjType="8;#1" ows_Created_x0020_Date="8;#2012-07-11 08:21:10" ows_ProgId="8;#" ows_FileLeafRef="8;#folder1" ows_PermMask="0x7fffffffffffffff" ows_FileRef="8;#sites/AAAAA0169/Test/vDocLib/folder1" ows_MetaInfo="8;#" ows_Last_x0020_Modified="8;#2012-07-11 08:21:10"/>

For Added file:

<z:row ows_DocIcon="sql" ows_LinkFilename="test.sql" ows_Modified="2012-07-11 08:33:49" ows_Editor="53;#Induvasi, Veeralaxminarayana SOMLOG-BNGLOG" ows_test="aa" ows_Shell_x0020_SharePoint_x0020_SAEF_x0020_Business="1;#Projects & Technology" ows__ModerationStatus="0" ows__Level="1" ows_ID="10" ows_UniqueId="10;#{7A8C0613-AFEC-4C63-8874-9C8C9921C816}" ows_owshiddenversion="3" ows_FSObjType="10;#0" ows_Created_x0020_Date="10;#2012-07-11 08:33:38" ows_ProgId="10;#" ows_FileLeafRef="10;#test.sql" ows_PermMask="0x7fffffffffffffff" ows_FileRef="10;#sites/AAAAA0169/Test/vDocLib/test.sql" ows_MetaInfo="10;#vti_contentversionisdirty:BW|false
vti_parserversion:SR|14.0.0.6117
Shell SharePoint SAEF LegalEntityTaxHTField0:SW|Shell Global Solutions|55cdefd7-04aa-4241-b39b-0dc3526b1f77
vti_contenttag:SW|{7A8C0613-AFEC-4C63-8874-9C8C9921C816},2,1
Shell SharePoint SAEF SecurityClassification:SW|10;#Restricted|21aa7f98-4035-4019-a764-107acb7269af
Shell SharePoint SAEF BusinessProcess:SW|8;#IT Project Delivery|b0369313-618e-46dc-b841-86c955d7e44c
vti_author:SR|i:0#.w|asia-pac\\v.induvasi
Shell SharePoint SAEF BusinessProcessTaxHTField0:SW|IT Project Delivery|b0369313-618e-46dc-b841-86c955d7e44c
Shell SharePoint SAEF WorkgroupIDTaxHTField0:SW|ITS IT SOLUTIONS DELIVERY - 11286|b0bab0af-8422-44cf-bdd6-13d6148a6daf
Shell SharePoint SAEF BusinessTaxHTField0:SW|Projects & Technology|71ef976b-0896-446b-8541-fe6e77f226a6
f20a57455b2440329628d1ed14beb17a:SW|Deliver Solutions|5538e1ef-ca09-4371-b859-92a9dbd93b32
Shell SharePoint SAEF DocumentStatus:SW|13;#Draft|1c86f377-7d91-4c95-bd5b-c18c83fe0aa5
Shell SharePoint SAEF GlobalFunctionTaxHTField0:SW|IT/IM|d388b442-0f35-4ef7-bb6d-ea4386749e1a
vti_modifiedby:SR|i:0#.w|asia-pac\\v.induvasi
vti_foldersubfolderitemcount:IR|0
Shell SharePoint SAEF Language:SW|6;#English|bd3ad5ee-f0c3-40aa-8cc8-36ef09940af3
vti_docstoreversion:IR|2
vti_metainfoversion:IW|3
Shell SharePoint SAEF ExportControlClassificationTaxHTField0:SW|Not Subject to EAR - No disclosure of technology|2ac8835e-0587-4096-a6e2-1f68da1e6cb3
ContentTypeId:SW|0x010100D5A611F60041F140B04070BBFF81FFE1
_dlc_DocId:SW|AAAAA0169-103-10
vti_title:SW|
Shell SharePoint SAEF LegalEntity:SW|4;#Shell Global Solutions|55cdefd7-04aa-4241-b39b-0dc3526b1f77
Shell SharePoint SAEF Business:SW|1;#Projects & Technology|71ef976b-0896-446b-8541-fe6e77f226a6
TaxCatchAll:SW|13;#Draft|1c86f377-7d91-4c95-bd5b-c18c83fe0aa5;#12;#Deliver Solutions|5538e1ef-ca09-4371-b859-92a9dbd93b32;#11;#Project Delivery|c9de3b69-09e7-403f-a718-1a1fcc1dfc16;#10;#Restricted|21aa7f98-4035-4019-a764-107acb7269af;#9;#Not Subject to EAR - No disclosure of technology|2ac8835e-0587-4096-a6e2-1f68da1e6cb3;#8;#IT Project Delivery|b0369313-618e-46dc-b841-86c955d7e44c;#7;#UNITED STATES|6c4ad875-5af6-45fb-9ae9-62dd1609b327;#6;#English|bd3ad5ee-f0c3-40aa-8cc8-36ef09940af3;#5;#ITS IT SOLUTIONS DELIVERY - 11286|b0bab0af-8422-44cf-bdd6-13d6148a6daf;#4;#Shell Global Solutions|55cdefd7-04aa-4241-b39b-0dc3526b1f77;#3;#IT/IM|d388b442-0f35-4ef7-bb6d-ea4386749e1a;#2;#Technical IT & IM|ffe12cfe-77b7-417c-876c-64d7aa485bc7;#1;#Projects & Technology|71ef976b-0896-446b-8541-fe6e77f226a6
_dlc_DocIdUrl:SW|https://eu001-sp.shell.com/sites/AAAAA0169/Test/_layouts/DocIdRedir.aspx?ID=AAAAA0169-103-10, AAAAA0169-103-10
Shell_x0020_SharePoint_x0020_SIS_x0020_ITDomain:SW|11;#Project Delivery|c9de3b69-09e7-403f-a718-1a1fcc1dfc16
Shell SharePoint SAEF BusinessUnitRegion:SW|2;#Technical IT & IM|ffe12cfe-77b7-417c-876c-64d7aa485bc7
Shell SharePoint SAEF GlobalFunction:SW|3;#IT/IM|d388b442-0f35-4ef7-bb6d-ea4386749e1a
n92ef52f300f4e41a6974dff859bb69a:SW|Project Delivery|c9de3b69-09e7-403f-a718-1a1fcc1dfc16
Shell SharePoint SAEF LanguageTaxHTField0:SW|English|bd3ad5ee-f0c3-40aa-8cc8-36ef09940af3
Shell SharePoint SAEF WorkgroupID:SW|5;#ITS IT SOLUTIONS DELIVERY - 11286|b0bab0af-8422-44cf-bdd6-13d6148a6daf
Shell SharePoint SAEF CountryOfJurisdictionTaxHTField0:SW|UNITED STATES|6c4ad875-5af6-45fb-9ae9-62dd1609b327
Shell SharePoint SAEF SecurityClassificationTaxHTField0:SW|Restricted|21aa7f98-4035-4019-a764-107acb7269af
vti_folderitemcount:IR|0
Shell SharePoint SAEF CountryOfJurisdiction:SW|7;#UNITED STATES|6c4ad875-5af6-45fb-9ae9-62dd1609b327
Shell SharePoint SAEF DocumentStatusTaxHTField0:SW|Draft|1c86f377-7d91-4c95-bd5b-c18c83fe0aa5
Shell SharePoint SAEF BusinessUnitRegionTaxHTField0:SW|Technical IT & IM|ffe12cfe-77b7-417c-876c-64d7aa485bc7
test:SW|aa
Shell_x0020_SharePoint_x0020_SIS_x0020_ITDMProcess:SW|12;#Deliver Solutions|5538e1ef-ca09-4371-b859-92a9dbd93b32
Shell SharePoint SAEF ExportControlClassification:SW|9;#Not Subject to EAR - No disclosure of technology|2ac8835e-0587-4096-a6e2-1f68da1e6cb3
_dlc_DocIdItemGuid:SW|7a8c0613-afec-4c63-8874-9c8c9921c816
" ows_Last_x0020_Modified="10;#2012-07-11 08:33:50"/>

i think we need to do some thing we need to do with  metainfo field So i tried  this <Field Name='MetaInfo'>""</Field>.But still its not working

Jun 24, 2013 at 1:31 PM
I want to update picture library items (delete from one folder and create/add this item in another folder) can you help me to do this using jQuery
Coordinator
Jun 27, 2013 at 5:54 PM
smly:

You should be able to use UpdateListItems to change the folder location, but I'm not sure if I've ever done it myself.

M.