Cómo crear una carpeta en la biblioteca de documentos de SharePoint 2010 usando métodos de descanso o HTTP
Pregunta
Estoy usando Java para hacer llamadas a la interfaz de REST de SharePoint para obtener elementos de la lista de una biblioteca de documentos y métodos HTTP para cargar documentos a SharePoint.Pero, estoy teniendo dificultades para tratar de averiguar cómo crear una carpeta programáticamente usando los métodos de descanso o HTTP.Cada vez que intento hacer una publicación con los datos de un artículo, obtengo el siguiente error
Encabezado de bichas no válidas para blobs.Los encabezados de Slug deben ser una URL de servidor o web relativa del archivo.Los URL relativos al servidor comienzan con una barra directa (\ "/ \").
¿Alguien puede ayudar?
Muchas gracias de antemano.
DINESH
Solución
Get items
To get all items you can either call this url:
http://yourhost/_vti_bin/ListData.svc/YourLib
Or you can call this url:
http://yourhost/_vti_bin/Lists.asmx
with this payload:
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
<listName>YourLib</listName>
</GetListItems>
</soap:Body>
</soap:Envelope>
Create folder
I can't say if this is the correct approach, but it should work.
It uses Batch
which I find a bit scary, but I have used this myself with success.
To create a folder you can call this url:
http://yourhost/sites/wf/_vti_bin/Lists.asmx
with this payload:
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
<listName>YourLib</listName>
<updates>
<Batch OnError='Continue' PreCalc='TRUE' ListVersion='0' >
<Method ID='1' Cmd='New'>
<Field Name='FSObjType'>1</Field>
<Field Name='BaseName'>YourFolder</Field>
</Method>
</Batch>
</updates>
</UpdateListItems>
</soap:Body>
</soap:Envelope>
Otros consejos
As Tommy pointed out and according to 3.1.4.3 Document:
Inserting new documents to a document library involve sending a POST request containing the contents of the document to the EntitySet representing the document library. The protocol client MUST include the SLUG header (as specified in RFC5023 section 9.7) whose value is the name of the file that is being created in their POST requests.
How to create Folder via REST in SharePoint 2010
function createFolder(webUrl,listName,folderName,folderPath, success, failure) {
var folderPayload = {
'ContentType': 'Folder',
'Title' : folderName,
'Path' : folderPath
};
$.ajax({
url: webUrl + "/_vti_bin/listdata.svc/" + listName,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(folderPayload),
headers: {
"Accept": "application/json;odata=verbose",
"Slug": folderPath + "/" + folderName + "|0x0120"
},
success: function (data) {
success(data.d);
},
error: function (data) {
failure(data.responseJSON.error);
}
});
}
Examples:
1 Create root folder named Orders
in the Documents
library
createFolder('https://intranet.contoso.com','Documents','Orders', '/Shared Documents',function(folder){
console.log('Folder ' + folder.Name + ' has been created succesfully');
},
function(error){
console.log(JSON.stringify(error));
}
);
2 Create sub folder named 2014
under the folder Orders
in the Documents
library
createFolder('https://intranet.contoso.com','Documents','2014', '/Shared Documents/Orders',function(folder){
console.log('Folder ' + folder.Name + ' has been created succesfully');
},
function(error){
console.log(JSON.stringify(error));
}
);
I don't have high enough reputation to comment, but I already gave the answer to this over here: Folder creation in Document library using listdata.svc
On creating folders in lists, its the same way, but without the "slug" header and you just add "/Lists" to beginning of the "Path" property.