Friday, 24 August 2018

Console application for office 365 and SharePoint 2013 server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Configuration;
using System.Collections.Specialized;
using System.Net;

namespace DemoConsole
{
    class Program
    {
        static string Intranet_userName = ConfigurationManager.AppSettings["Intranet_userName"].ToString();
        static string Intranet_password = ConfigurationManager.AppSettings["Intranet_password"].ToString();
        static string IntranetUrl = ConfigurationManager.AppSettings["IntranetUrl"].ToString();
        static string userName = ConfigurationManager.AppSettings["Online_userName"].ToString();
        static string pwd = ConfigurationManager.AppSettings["Online_password"].ToString();
        static SecureString password = FetchPasswordFromConsole(pwd);
        static string OnlineUrl = ConfigurationManager.AppSettings["OnlineUrl"].ToString();
     
        static string SchedulerLog = ConfigurationManager.AppSettings["SchedulerLog"].ToString();
        static String TimeZone = ConfigurationManager.AppSettings["TimeZone"].ToString();
        private static TimeZoneInfo Pacific_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById(TimeZone);
        static ListItemCollection OnlineItems;
        static ListItemCollection IntranetItem;
   
        static void Main(string[] args)
        {
            Console.WriteLine("Execution started");
            //Reading list data from sharepoint 2010 list
            NetworkCredential _myCredentials = new NetworkCredential(Intranet_userName, Intranet_password);
            ClientContext _clientContext = new ClientContext(IntranetUrl);
            _clientContext.Credentials = _myCredentials;
            List iList = _clientContext.Web.Lists.GetByTitle("Test");
            CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery();
            IntranetItem = iList.GetItems(camlQuery);
            _clientContext.Load(IntranetItem);
            _clientContext.ExecuteQuery();

            for (int j = 0; j < IntranetItem.Count; j++)
            {
Console.WriteLine(IntranetItem[j]["ID"]);
            }

// connect office 365 siteUrl

using (var pmisCtx = new ClientContext(OnlineUrl))
                    {

                        pmisCtx.Credentials = new SharePointOnlineCredentials(userName, password);
                        List oList = pmisCtx.Web.Lists.GetByTitle("TestList");
                        CamlQuery OcamlQuery = CamlQuery.CreateAllItemsQuery();
                        OnlineItems = oList.GetItems(OcamlQuery);
                        pmisCtx.Load(OnlineItems);
                        pmisCtx.ExecuteQuery();
}

for (int i = 0; j < OnlineItems.Count; j++)
{
Console.WriteLine(OnlineItems[i]["ID"]);
}

           var intranetitemscount= IntranetItem.Count;
   var onlineitemscount= OnlineItems.Count;
           Console.WriteLine(intranetitemscount);
   Console.WriteLine(onlineitemscount);
           Console.Read();
        }

        private static SecureString FetchPasswordFromConsole(string pwd)
        {
            string password = pwd;
            var securePassword = new SecureString();
            foreach (char c in password)
                securePassword.AppendChar(c);
            securePassword.MakeReadOnly();
            return securePassword;
        }

    }
}

Add below information in AppConfig file :-

<appSettings>
    <add key="Intranet_userName" value="username" />
    <add key="Intranet_password" value="Password" />
    <add key="IntranetUrl" value="http://siteUrl" />
    <add key="TestList" value="TestList" />
    <add key="SchedulerLog" value="TestLog" />
    <add key="TimeZone" value="Pacific Standard Time" />
  </appSettings>

 Add below reference dll :-

 Microsoft.SharePoint.Client.dll
 Microsoft.SharePoint.Client.Runtime.dll
 System.Configuration.dll

No comments:

Post a Comment