+ All Categories
Home > Documents > · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item =>...

· Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item =>...

Date post: 08-Mar-2018
Category:
Upload: hathuy
View: 218 times
Download: 4 times
Share this document with a friend
24
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
Transcript
Page 1: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

Page 2: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

using System.Windows.Forms;

using System.Security;

using Microsoft.SharePoint.Client;

using SP = Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client.WorkflowServices;

using Microsoft.SharePoint.Client.Workflow;

using System.Collections;

using System.IO;

namespace ConnectOffice365

{

public partial class Form1 : System.Windows.Forms.Form

{

string strSiteURL;

CheckBox HeaderCheckBox = null;

bool IsHeaderCheckBoxClicked = false;

int TotalCheckBoxes = 0;

int TotalCheckedCheckBoxes = 0;

// check chkbxWFNames;

#region Page Load

public Form1()

{

try

{

InitializeComponent();

}

catch (Exception ex)

{

Page 3: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

lblError.Visible = true;

lblError.Text = "Page Load : " + ex.ToString();

}

}

#endregion

#region Grid View

private void dtGrVItmes_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

if (!IsHeaderCheckBoxClicked)

RowCheckBoxClick((DataGridViewCheckBoxCell)dtGrVItmes[e.ColumnIndex, e.RowIndex]);

}

private void dtGrVItmes_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (dtGrVItmes.CurrentCell is DataGridViewCheckBoxCell)

dtGrVItmes.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

private void dtGrVItmes_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == -1 && e.ColumnIndex == 0)

ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);

}

private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)

{

if (RCheckBox != null)

{

//Modifiy Counter;

Page 4: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

if ((bool)RCheckBox.Value && TotalCheckedCheckBoxes < TotalCheckBoxes)

TotalCheckedCheckBoxes++;

else if (TotalCheckedCheckBoxes > 0)

TotalCheckedCheckBoxes--;

//Change state of the header CheckBox.

if (TotalCheckedCheckBoxes < TotalCheckBoxes)

HeaderCheckBox.Checked = false;

else if (TotalCheckedCheckBoxes == TotalCheckBoxes)

HeaderCheckBox.Checked = true;

}

}

private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Space)

HeaderCheckBoxClick((CheckBox)sender);

}

private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)

{

HeaderCheckBoxClick((CheckBox)sender);

}

private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == -1 && e.ColumnIndex == 0)

ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);

}

private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)

{

Page 5: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

try

{

//Get the column header cell bounds

Rectangle oRectangle = this.dtGrVItmes.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);

Point oPoint = new Point();

oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;

oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;

//Change the location of the CheckBox to make it stay on the header

HeaderCheckBox.Location = oPoint;

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "ResetHeaderCheckBoxLocation : " + ex.ToString();

}

}

private void HeaderCheckBoxClick(CheckBox HCheckBox)

{

IsHeaderCheckBoxClicked = true;

foreach (DataGridViewRow Row in dtGrVItmes.Rows)

((DataGridViewCheckBoxCell)Row.Cells["chkBxSelect"]).Value = HCheckBox.Checked;

dtGrVItmes.RefreshEdit();

TotalCheckedCheckBoxes = HCheckBox.Checked ? TotalCheckBoxes : 0;

IsHeaderCheckBoxClicked = false;

}

#endregion

Page 6: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

#region Load Site Details

private void btnGetDetails_Click(object sender, EventArgs e)

{

LoadSite();

}

public void LoadSite()

{

try

{

string strURL = txtSiteURL.Text.ToString();

string userName = txtUserName.Text.ToString();

// string password = "";

SecureString password = FetchPasswordFromConsole(txtPassword.Text);

// string strSelectedName = ddlListName.SelectedValue.ToString();

// string strWFName = txtWorkflowName.Text.ToString();

strSiteURL = txtSiteURL.Text.ToString();

string strResult = string.Empty;

using (var context = new ClientContext(strURL))

{

context.Credentials = new SharePointOnlineCredentials(userName, password);

Web web = context.Web;

context.Load(web.Lists,

lists => lists.Include(list => list.Title,

list => list.Id));

context.ExecuteQuery();

// List<ListLib> LstLib;

List<string> LstLib = new List<string>();

//ddlListName.Items.Add("Select");

Page 7: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

LstLib.Add("Select");

foreach (List list in web.Lists)

{

//strResult += "List title is: " + list.Title;

LstLib.Add(list.Title.ToString());

// Console.WriteLine("List title is: " + list.Title);

}

ddlListName.DataSource = LstLib;

}

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "LoadSite : " + ex.ToString();

}

}

private static SecureString FetchPasswordFromConsole(string password)

{

var securePassword = new SecureString();

foreach (char c in password)

securePassword.AppendChar(c);

securePassword.MakeReadOnly();

return securePassword;

}

#endregion

Page 8: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

#region Drop down list selection

private void ddlListName_SelectedIndexChanged(object sender, EventArgs e)

{

try

{

if (ddlListName.SelectedValue.ToString() != "Select")

{

AddHeaderCheckBox();

HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);

HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);

dtGrVItmes.CellValueChanged += new DataGridViewCellEventHandler(dtGrVItmes_CellValueChanged);

dtGrVItmes.CurrentCellDirtyStateChanged += new EventHandler(dtGrVItmes_CurrentCellDirtyStateChanged);

dtGrVItmes.CellPainting += new DataGridViewCellPaintingEventHandler(dtGrVItmes_CellPainting);

GetSelectedList();

// Do something here

}

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "ddlListName_SelectedIndexChanged : " + ex.ToString();

}

}

Page 9: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

public void GetSelectedList()

{

try

{

string strURL = txtSiteURL.Text.ToString();

string userName = txtUserName.Text.ToString();

// string password = "";

SecureString password = FetchPasswordFromConsole(txtPassword.Text);

string strSelectedName = ddlListName.SelectedValue.ToString();

string strWFName = txtWorkflowName.Text.ToString();

strSiteURL = txtSiteURL.Text.ToString();

string strResult = string.Empty;

using (var vContext = new ClientContext(strURL))

{

vContext.Load(vContext.Web);

Microsoft.SharePoint.Client.Web web = vContext.Web;

vContext.Credentials = new SharePointOnlineCredentials(userName, password);

List list = vContext.Web.Lists.GetByTitle(strSelectedName);

var items = list.GetItems(

new CamlQuery()

{

ViewXml = @"<View Scope='RecursiveAll'><ViewFields><FieldRef Name='Title' /><FieldRef Name='FileLeafRef' /><FieldRef Name='ID' /><FieldRef Name='ContentType' /></ViewFields><Query>

<Where><IsNotNull><FieldRef Name='File_x0020_Type' /></IsNotNull></Where>

</Query></View>"

});

vContext.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser));

Page 10: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

vContext.ExecuteQuery();

DataTable dtList = new DataTable();

dtList.Columns.Add("S NO", typeof(Int32));

dtList.Columns.Add("ID", typeof(string));

dtList.Columns.Add("Title", typeof(string));

dtList.Columns.Add("FileLeafRef", typeof(string));

dtList.Columns.Add("FileStatus", typeof(string));

dtList.Columns.Add("CheckOutbyUser", typeof(string));

//dtList.Columns.Add("Check", typeof(bool));

//DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();

// dtList.Columns.Add("Check",chk);

int iCnt = 1;

foreach (var item in items)

{

DataRow dtrow = dtList.NewRow(); // Create New Row

dtrow["S NO"] = iCnt; //Bind Data to Columns

dtrow["ID"] = String.IsNullOrEmpty(item.Id.ToString()) ? "" : item.Id.ToString(); // oListItem.Id.ToString();

dtrow["Title"] = item["Title"] == null ? "" : item["Title"].ToString(); // oListItem["Title"].ToString();

dtrow["FileLeafRef"] = item["FileLeafRef"] == null ? "" : item["FileLeafRef"].ToString(); // oListItem["FileLeafRef"].ToString();

if (item.File.CheckOutType != CheckOutType.None)

{

dtrow["FileStatus"] = "Check OUT";

dtrow["CheckOutbyUser"] = item.File.CheckedOutByUser.Title.ToString();

Page 11: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

}

else

{

dtrow["FileStatus"] = "";

}

dtList.Rows.Add(dtrow);

iCnt++;

}

DataGridViewCheckBoxColumn dgvCmb = new DataGridViewCheckBoxColumn();

dgvCmb.ValueType = typeof(bool);

dgvCmb.Name = "chkBxSelect";

dgvCmb.HeaderText = "";

dtGrVItmes.Columns.Add(dgvCmb);

dtGrVItmes.DataSource = dtList;

}

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "GetSelectedList : " + ex.ToString();

}

}

private void AddHeaderCheckBox()

{

try

{

Page 12: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

HeaderCheckBox = new CheckBox();

HeaderCheckBox.Size = new Size(15, 15);

//Add the CheckBox into the DataGridView

this.dtGrVItmes.Controls.Add(HeaderCheckBox);

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "AddHeaderCheckBox : " + ex.ToString();

}

}

#endregion

#region CheckIn Files to Doc Lib

private void btnSubmit_Click(object sender, EventArgs e)

{

try

{

if (dtGrVItmes.Rows.Count > 0)

{

string strURL = txtSiteURL.Text.ToString();

string userName = txtUserName.Text.ToString();

// string password = "";

SecureString password = FetchPasswordFromConsole(txtPassword.Text);

string strSelectedName = ddlListName.SelectedValue.ToString();

string strWFName = txtWorkflowName.Text.ToString();

strSiteURL = txtSiteURL.Text.ToString();

string strResult = string.Empty;

using (var vContext = new ClientContext(strURL))

{

Page 13: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

vContext.Load(vContext.Web);

Microsoft.SharePoint.Client.Web web = vContext.Web;

vContext.Credentials = new SharePointOnlineCredentials(userName, password);

foreach (DataGridViewRow row in dtGrVItmes.Rows)

{

int jID;

string strID = row.Cells["ID"].Value == null ? "" : row.Cells["ID"].Value.ToString();

if (strID != null)

{

if (Convert.ToBoolean(row.Cells[0].Value))

{

if (Int32.TryParse(strID, out jID))

{

UpdateItems(vContext, strSelectedName, jID, strWFName);

}

}

}

}

}

}

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "btnSubmit_Click : " + ex.ToString();

}

}

public void UpdateItems(ClientContext ctx, string strListName, int itmID, string strWFName)

{

Page 14: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

try

{

List listT = ctx.Web.Lists.GetByTitle(strListName);//Almas

ctx.Load(listT);

Web web = ctx.Web;

ctx.Load(web);

List list = ctx.Web.Lists.GetByTitle(strListName);

WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(ctx, web);

WorkflowInstanceService wfInstanceService = wfServicesManager.GetWorkflowInstanceService();

var items = list.GetItems(

new CamlQuery()

{

//ViewXml = @"<View Scope='RecursiveAll'><ViewFields><FieldRef Name='Title' /><FieldRef Name='FileLeafRef' /><FieldRef Name='ID' /><FieldRef Name='ContentType' /></ViewFields><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itmID + "</Value></Eq></Where></Query></View>"

ViewXml = @"<View Scope='RecursiveAll'><ViewFields><FieldRef Name='Title' /><FieldRef Name='FileLeafRef' /><FieldRef Name='ID' /><FieldRef Name='ContentType' /></ViewFields><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itmID + "</Value></Eq></Where></Query></View>"

}

);

ctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser));

ctx.ExecuteQuery();

foreach (var item in items)

{

if (item.File.CheckOutType != CheckOutType.None)

{

////// Forcely Cehck in file

item.File.CheckIn("", CheckinType.MajorCheckIn);

Page 15: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

item.Update();

}

}

list.Update();

ctx.ExecuteQuery();

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "UpdateItems : " + ex.ToString();

}

}

#endregion

#region WF Cancelling

private void btnCancellingWF_Click(object sender, EventArgs e)

{

if (txtWorkflowName.Text == "")

{

lblError.Text = "Please, Enter Workflow Name";

}

else

{

lblError.Text = "";

GetListWFAssociations(); // Set "No New Insatnce" wf property on List

this.Invalidate();

}

}

Page 16: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

public void GetListWFAssociations()

{

try

{

string strURL = txtSiteURL.Text.ToString();

string userName = txtUserName.Text.ToString();

// string password = "";

SecureString password = FetchPasswordFromConsole(txtPassword.Text);

string strSelectedName = ddlListName.SelectedValue.ToString();

string strWFName = txtWorkflowName.Text.ToString();

strSiteURL = txtSiteURL.Text.ToString();

string strResult = string.Empty;

using (var vContext = new ClientContext(strURL))

{

//

List listT = vContext.Web.Lists.GetByTitle(strSelectedName);

vContext.Credentials = new SharePointOnlineCredentials(userName, password);

Web web = vContext.Web;

vContext.Load(web);

vContext.Load(listT, w => w.WorkflowAssociations, w => w.Title, w => w.BaseType);

vContext.ExecuteQuery();

var wfa = listT.WorkflowAssociations;

foreach (var associ in wfa)

{

if (associ.Name == strWFName)

{

if (associ.Enabled == true)

{

associ.Enabled = false;

Page 17: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

associ.Update();

}

}

}

// wfa.Update();//Update association config

listT.Update();//Update the list

vContext.Load(wfa);

vContext.ExecuteQuery();

}

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "UpdateItems : " + ex.ToString();

}

}

#endregion

#region Bulk Upload

private void btnUploadFolder_Click(object sender, EventArgs e)

{

FolderUpload();

}

public void FolderUpload()

{

try

{

string strURL = txtSiteURL.Text.ToString();

Page 18: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

string userName = txtUserName.Text.ToString();

string strFolderLocation = txtFolderLocation.Text.ToString();

SecureString password = FetchPasswordFromConsole(txtPassword.Text);

string strSelectedName = "FolderUpload";

strSiteURL = txtSiteURL.Text.ToString();

string strResult = string.Empty;

using (var context = new ClientContext(strURL))

{

context.Credentials = new SharePointOnlineCredentials(userName, password);

Web web = context.Web;

UploadFoldersRecursively(context, strFolderLocation, strSelectedName);

}

}

catch (Exception ex)

{

lblError.Visible = true;

lblError.Text = "LoadSite : " + ex.ToString();

}

}

public static void UploadFolder(ClientContext clientContext, System.IO.DirectoryInfo folderInfo, Folder folder)

{

System.IO.FileInfo[] files = null;

System.IO.DirectoryInfo[] subDirs = null;

try

{

files = folderInfo.GetFiles("*.*");

}

Page 19: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

catch (UnauthorizedAccessException e)

{

//Console.WriteLine(e.Message);

}

catch (System.IO.DirectoryNotFoundException e)

{

//Console.WriteLine(e.Message);

}

if (files != null)

{

foreach (System.IO.FileInfo fi in files)

{

// Console.WriteLine(fi.FullName);

clientContext.Load(folder);

clientContext.ExecuteQuery();

UploadDocument(clientContext, fi.FullName, folder.ServerRelativeUrl + "/" + fi.Name);

}

subDirs = folderInfo.GetDirectories();

foreach (System.IO.DirectoryInfo dirInfo in subDirs)

{

Folder subFolder = folder.Folders.Add(dirInfo.Name);

clientContext.ExecuteQuery();

UploadFolder(clientContext, dirInfo, subFolder);

}

}

}

Page 20: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

public static void UploadDocument(ClientContext clientContext, string sourceFilePath, string serverRelativeDestinationPath)

{

using (var fs = new FileStream(sourceFilePath, FileMode.Open))

{

var fi = new FileInfo(sourceFilePath);

Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeDestinationPath, fs, true);

}

}

public static void UploadFoldersRecursively(ClientContext clientContext, string sourceFolder, string destinationLigraryTitle)

{

Web web = clientContext.Web;

var query = clientContext.LoadQuery(web.Lists.Where(p => p.Title == destinationLigraryTitle));

clientContext.ExecuteQuery();

List documentsLibrary = query.FirstOrDefault();

var folder = documentsLibrary.RootFolder;

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sourceFolder);

clientContext.Load(documentsLibrary.RootFolder);

clientContext.ExecuteQuery();

folder = documentsLibrary.RootFolder.Folders.Add(di.Name);

clientContext.ExecuteQuery();

UploadFolder(clientContext, di, folder);

}

#endregion

Page 21: · Web viewctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); ctx.ExecuteQuery(); foreach (var item in items) {if (item.File.CheckOutType

}

}


Recommended