Wednesday, 15 August 2012

c# - When renaming a file in SharePoint via client object model, the filename gets trimmed if there's a dot in between -



c# - When renaming a file in SharePoint via client object model, the filename gets trimmed if there's a dot in between -

i wrote little application using sharepoint client object model, renames files within sharepoint 2010 document library. works well, except if filename should contain dot somewhere in between, get's trimmed, starting dot.

for example, when new filename should "my fi.le name" ends "my fi" in sharepoint. extension of file (.pdf in case) stays right way.

here's i'm doing (in general):

clientcontext clientcontext = new clientcontext("http://sp.example.com/thesite); list list = clientcontext.web.lists.getbytitle("mydoclibrary"); listitemcollection col = list.getitems(camlquery.createallitemsquery()); clientcontext.load(col); clientcontext.executequery(); foreach (var doc in col) { if (doc.filesystemobjecttype == filesystemobjecttype.file) { doc["fileleafref"] = "my fi.le name"; doc.update(); clientcontext.executequery(); } }

when i'm renamig file in sharepoint manually via browser (edit properties), works should: dot stays , filename won't trimmed @ all.

is "fileleafref" wrong property? ideas what's cause here?

using fileleafref property possible update file name without extension.

how rename file using sharepoint csom

use file.moveto method rename file:

public static void renamefile(clientcontext ctx,string fileurl,string newname) { var file = ctx.web.getfilebyserverrelativeurl(fileurl); ctx.load(file.listitemallfields); ctx.executequery(); file.moveto(file.listitemallfields["filedirref"] + "/" + newname, moveoperations.overwrite); ctx.executequery(); }

usage

using (var ctx = new clientcontext(weburl)) { renamefile(ctx, "/shared documents/user guide.docx", "user guide 2013.docx"); }

c# sharepoint client-object-model

No comments:

Post a Comment