0

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.

Bumblebee
  • 179
  • 1
  • 16
  • What is recorded in your ASSIST.ERROR_LOGS table? – billinkc Apr 23 '20 at 16:17
  • Has this ever worked in production or is this the first time it's been run in that environment? – billinkc Apr 23 '20 at 16:19
  • Nothing has recorded on ASSIST.ERROR_LOGS. But I have checked the Package logs and it says the Exception Has been the target of an invocation. Apart from that, I see no logs. This is the first time I deployment this type of code. @billinkc – Bumblebee Apr 23 '20 at 16:20
  • And final question: has the Microsoft.SharePoint.Client dll been installed into the global assembly cache, GAC on the production server? Knowing nothing of this dll, is it 32 bit, 64 bit or independent? If it's 32 or 64, ensure you're executing packages with the same version of dtexec. The default during development is 32 bit while SQL Agent job default is 64 bit – billinkc Apr 23 '20 at 16:22
  • I don't know if it is installed or not on the Production Server. Both package and Production Server has a 64-bit environment. @billinkc – Bumblebee Apr 23 '20 at 16:26
  • Either the catch block was unable to perform the insert (connection was bad, failed permissions, etc) or, more likely given the SP dll dependency, the code is unable to run on the server as the required assembly is not present. Building/compiling an SSIS script task does not embed assemblies with it. – billinkc Apr 23 '20 at 16:32
  • the catch block is working with other packages. As you said the Production server might not have SP dll assembly or The Service account of the Production server does not have enough permissions on the SharePoint Site. I will investigate on my side with Admin. Thank you @billinkc – Bumblebee Apr 23 '20 at 16:40

0 Answers0