Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to delete or remove only text files from share point in C# or SSIS script???
Posted

follow this link to all share point report DML operations http://www.c-sharpcorner.com/tags/DDL-and-DML-Operations--Sharepoint[^]
 
Share this answer
 
// script code for downloading attachments from share point as well as remove attachments of particular files (like only .txt files , or only .csv .. etc)
public void Main()
{
try
{
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
String myFileExtension = "";
using (SPSite oSite = new SPSite("http://servername/sharepointname/"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList list = oWeb.Lists["listName"];

SPListItemCollection oItemCol = list.GetItems();
foreach (SPListItem oItem in oItemCol)
{
string strID = Convert.ToString(oItem["ID"]);
SPFolder folder = oWeb.Folders["Lists"].SubFolders["ListName"].SubFolders["Attachments"].SubFolders[strID];
foreach (SPFile file in folder.Files)
{
FileInfo myFileinfo = new FileInfo(file.Name);
myFileExtension = myFileinfo.Extension.ToLower();
//MessageBox.Show("myFileExtension:" + myFileExtension);
String sFileextension = (Dts.Variables["fileformat"].Value.ToString().Trim()).ToLower();
if (myFileExtension.Equals(sFileextension))
{
WebClient client1 = new WebClient();
client1.Credentials = System.Net.CredentialCache.DefaultCredentials;
FileStream outStream = new FileStream(@"C:\\destination_folder\" + file.Name, FileMode.Create);
byte[] fileData = file.OpenBinary();
outStream.Write(fileData, 0, fileData.Count());
outStream.Close();
oItem.Attachments.Delete(file.Name);
oItem.Update();
}

}
}
}
}
//});
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}


// only administrator can executes this code, bcz sharepoint list and attachments are assigned to different users, as user , user can't download all attachments from share point.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900