I'm trying to delete Microsoft Sharepoint lists items using the SSIS C# script task. It's working fine on my local computer. I installed Microsoft.SharePoint. dll on my computer to do this task.
Below is what I have done on an SSIS Package script task.
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.Linq;
using System.Text;
using System.Net;
using System.Security;
using System.Data.Sql;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace ST_d55693ffa58249c9b326e10762830927
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
try
{
string siteUrl = "https://comapany.sharepoint.com/sites/sinfo/IT_TEAM/";
using (ClientContext clientContext = new ClientContext(siteUrl))
{
var passWord = new SecureString();
foreach (char c in "******************".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("user@comapany.com", passWord);
Web oWebsite = clientContext.Web;
List oList = oWebsite.Lists.GetByTitle("TEST_S");
ListItemCollectionPosition licp = null;
clientContext.Load(oList);
clientContext.ExecuteQuery();
int itemCount = oList.ItemCount;
while (true)
{
CamlQuery query = new CamlQuery();
query.ViewXml
= @"<View><ViewFields><FieldRef Name='ID'/></ViewFields><RowLimit>250</RowLimit></View>";
query.ListItemCollectionPosition = licp;
ListItemCollection items = oList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
licp = items.ListItemCollectionPosition;
itemCount -= items.Count;
foreach (ListItem itm in items.ToList())
{
itm.DeleteObject();
}
clientContext.ExecuteQuery();
if (licp == null)
{
break;
}
}
}
}
catch (Exception e)
{
SqlConnection con = new SqlConnection("Data Source=PROD_20;Initial Catalog=TEAM_DATA;Integrated Security=SSPI;");
con.Open();
string qry = "INSERT INTO ASSIST.ERROR_LOGS(PACKAGE,METHOD,MESSAGE,TIME_OCCURED) VALUES(@Package,@Method,@Message,@Time);";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.Add("@Package", SqlDbType.VarChar, 20);
cmd.Parameters.Add("@Method", SqlDbType.VarChar, 30);
cmd.Parameters.Add("@Message", SqlDbType.NVarChar, 255);
cmd.Parameters.Add("@Time", SqlDbType.DateTime);
cmd.Parameters["@Package"].Value = "PRE_SP";
cmd.Parameters["@Method"].Value = "PRE MAIN";
cmd.Parameters["@Message"].Value = e.Message.ToString();
cmd.Parameters["@Time"].Value = DateTime.Now;
cmd.ExecuteNonQuery();
con.Close();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
After deploying the package on to the Production server. It is throwing an exception like Exception has been thrown by the target of an invocation.
is this something to do with the Prod service account to have access on the Sharepoint Site.