Monday, August 12, 2013

Moving the Files in SharePoint

I am trying to achieve the file or files from the Shared Document library to one of my target list item's folder.
You can use this code where you want to attach the files to List Item on run time.

Get the Item's ID by passing into GetItemById method of the Items collection to get the specific Item and its attachment folder then execute the

The below code is just a rough workaround.You need to check the Item's existence before execute code on it.

SPListItem mYItem = list.Items.GetItemById(aB);
 SPFile file = mYItem.File;
var exactUrl = mYItem.Attachments.UrlPrefix;
 file.MoveTo(exactUrl + file.Name, SPMoveOperations.Overwrite, false);