Frage

I have a site with 1,000s of folders with a ".cd" extension to the name (still trying to track down why).

I'm trying to archive the site to a file share, but each folder with a ".cd" extension is blocked form moving for obvious reasons.

This extension is found on folders and their sub folders for varying levels of depth.

Is there anything I can run against this site to remove ".cd" from all items?

War es hilfreich?

Lösung

You can't change the name directly, but you can change the Title and FileLeafRef as explained in this thread. So here is my code. It relies on having jQuery to handle the REST calls.

First get all the folders that contain a .cd. There is no endsWith() filter for rest, so I did a "contains" using the substringof().

function getFolders(){

    var query = {
        "$select":"FileRef,FileLeafRef",
        "$filter":"FSObjType eq 1 and substringof('.cd',Title)",
        "$orderby":"FileRef desc"
    };

    // sort by the longest FileRef (url path) we want to work up from the bottom

    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getByTitle('"+libraryName+')/Items",
        type:"GET",
        data: query,
        headers:{'Accept':"application/json;odata=verbose"}
    })
    .then(function(data){
        return $.when(data.d.results);
    });
}

I've sorted this with the longest FileRef which is the path first. This is so we can work up from the bottom of the tree and won't change the name of a containing folder.

Using the trick mentioned above of changing the FileLeafRef and Title we can iterate through the folders. We're checking that the .cd is at the end.

function fixFolder(folders){

    // case insensitive ends in .cd
    var reg = /\.cd$/i;
    var folder;

    do {
        folder = folders.shift();
    } while (folders.length > 0 && !reg.test(folder.FileLeafRef));

    var name = folder.FileLeafRef.replace(reg,''); 

    var payload = {
        "__metadata":{"type":folder.__metadata.type},
        "FileLeafRef":name,
        "Title":name
    };

    return $.ajax({
        url:_spPageContextInfo.webAbsoluteUrl+"/_api/"+folder.__metadata.id,
        type:"POST",
        data:JSON.stringify(payload),
        headers:{
            'Accept':'application/json;odata=verbose',
            'Content-Type':'application/json;odata=verbose',
            'If-Match':folder.__metadata.etag,
            'X-HTTP-Method':'MERGE',
            'X-RequestDigest':$('#__REQUESTDIGEST').val()
        }
    })
    .then(function(){
        if(folders.length>0){
            fixFolder(folders);
        }
    });
}

Finally put it all together. This could be called on a document ready or made into a function.

getFolders()
    .then(fixFolder)
    .fail(function(err){
        console.log("There was an error");
        console.log(err);
    });

It runs in the client browser with the same permissions as whoever runs it. So if there are things you're not allowed to see or edit then it won't work.

Andere Tipps

PnP powershell to the rescue:

Iterate each site and each library, get each folder containing '.cd' and rename it to substring(0,indexof(.cd)-1)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top