/// <summary> /// </summary> private void bgw_DelSel(object sender, DoWorkEventArgs e) { var lstUserActionObjs = new List <UserActionObject>(); try { for (int i = 0; i < dataGridView1.Rows.Count; i++) { var k = 0; var userActionObj = new BandR.UserActionObject(); userActionObj.selected = Convert.ToBoolean(dataGridView1.Rows[i].Cells[k++].Value); userActionObj.id = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.name = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.descr = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.seq = Convert.ToInt32(dataGridView1.Rows[i].Cells[k++].Value); userActionObj.scriptSrc = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.scriptBlock = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); lstUserActionObjs.Add(userActionObj); } var targetSite = new Uri(tbSiteUrl.Text.Trim()); using (ClientContext ctx = new ClientContext(targetSite)) { ctx.Credentials = BuildCreds(); FixCtxForMixedMode(ctx); Site site = ctx.Site; ctx.Load(site, x => x.ServerRelativeUrl); ctx.ExecuteQuery(); tcout("Site loaded", site.ServerRelativeUrl); UserCustomActionCollection spActions = null; if (scope.IsEqual("Site Collection")) { spActions = ctx.Site.UserCustomActions; } else { spActions = ctx.Web.UserCustomActions; } ctx.Load(spActions); ctx.ExecuteQuery(); tcout("UserCustomActions Count", spActions.Count); for (int i = spActions.Count - 1; i >= 0; i--) { var curSpAction = spActions[i]; if (lstUserActionObjs.Any(x => Guid.Parse(x.id) == curSpAction.Id && x.selected)) { curSpAction.DeleteObject(); lstUserActionObjs.RemoveAll(x => Guid.Parse(x.id) == curSpAction.Id && x.selected); tcout("Deleting", curSpAction.Id, curSpAction.Name); } } if (ctx.HasPendingRequest) { ctx.ExecuteQuery(); tcout("Action(s) deleted."); } } } catch (Exception ex) { tcout(" *** ERROR", GetExcMsg(ex)); ErrorOccurred = true; } e.Result = new List <object>() { lstUserActionObjs }; }
private void bgw_SaveSelChanges(object sender, DoWorkEventArgs e) { var lstUserActionObjs = new List <UserActionObject>(); try { for (int i = 0; i < dataGridView1.Rows.Count; i++) { var k = 0; var userActionObj = new BandR.UserActionObject(); userActionObj.selected = Convert.ToBoolean(dataGridView1.Rows[i].Cells[k++].Value); userActionObj.id = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.name = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.descr = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.seq = Convert.ToInt32(dataGridView1.Rows[i].Cells[k++].Value); userActionObj.scriptSrc = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); userActionObj.scriptBlock = dataGridView1.Rows[i].Cells[k++].Value.SafeTrim(); lstUserActionObjs.Add(userActionObj); } var targetSite = new Uri(tbSiteUrl.Text.Trim()); using (ClientContext ctx = new ClientContext(targetSite)) { ctx.Credentials = BuildCreds(); FixCtxForMixedMode(ctx); Site site = ctx.Site; ctx.Load(site, x => x.ServerRelativeUrl); ctx.ExecuteQuery(); tcout("Site loaded", site.ServerRelativeUrl); UserCustomActionCollection spActions = null; if (scope.IsEqual("Site Collection")) { spActions = ctx.Site.UserCustomActions; } else { spActions = ctx.Web.UserCustomActions; } ctx.Load(spActions); ctx.ExecuteQuery(); tcout("UserCustomActions Count", spActions.Count); for (int i = spActions.Count - 1; i >= 0; i--) { var changed = false; var curSpAction = spActions[i]; var curGridAction = lstUserActionObjs.FirstOrDefault(x => Guid.Parse(x.id) == curSpAction.Id); // && x.selected if (curGridAction != null) { if (curGridAction.scriptSrc.IsNull() && curGridAction.scriptBlock.IsNull()) { // invalid option tcout(" *** Skipping update, cannot save CustomAction with both ScriptSrc and ScriptBlock filled", curGridAction.id, curGridAction.name); continue; } if (curSpAction.Name != curGridAction.name && !curGridAction.name.IsNull()) { curSpAction.Name = curGridAction.name; changed = true; } if (curSpAction.Description != curGridAction.descr) { curSpAction.Description = curGridAction.descr.IsNull() ? null : curGridAction.descr; changed = true; } if (curSpAction.Sequence != curGridAction.seq) { curSpAction.Sequence = curGridAction.seq <= 0 ? _defaultSeq : curGridAction.seq; changed = true; } if (curSpAction.ScriptSrc != curGridAction.scriptSrc) { curSpAction.ScriptSrc = curGridAction.scriptSrc.IsNull() ? null : curGridAction.scriptSrc; changed = true; } if (curSpAction.ScriptBlock != curGridAction.scriptBlock) { curSpAction.ScriptBlock = curGridAction.scriptBlock.IsNull() ? null : curGridAction.scriptBlock; changed = true; } if (changed) { curSpAction.Update(); } } } if (ctx.HasPendingRequest) { ctx.ExecuteQuery(); tcout("Action(s) updated."); } } } catch (Exception ex) { tcout(" *** ERROR", GetExcMsg(ex)); ErrorOccurred = true; } e.Result = new List <object>() { lstUserActionObjs }; }
/// <summary> /// </summary> private void bgw_LoadActions(object sender, DoWorkEventArgs e) { var lstUserActionObjs = new List <UserActionObject>(); try { var targetSite = new Uri(tbSiteUrl.Text.Trim()); using (ClientContext ctx = new ClientContext(targetSite)) { ctx.Credentials = BuildCreds(); FixCtxForMixedMode(ctx); Site site = ctx.Site; ctx.Load(site, x => x.ServerRelativeUrl); ctx.ExecuteQuery(); tcout("Site loaded", site.ServerRelativeUrl); UserCustomActionCollection spActions = null; if (scope.IsEqual("Site Collection")) { spActions = ctx.Site.UserCustomActions; } else { spActions = ctx.Web.UserCustomActions; } ctx.Load(spActions); ctx.ExecuteQuery(); tcout("UserCustomActions Count", spActions.Count); var spActionsSorted = spActions.ToList <UserCustomAction>().OrderBy(x => x.Sequence).ThenBy(x => x.Name); foreach (UserCustomAction spAction in spActionsSorted) { var userActionObj = new BandR.UserActionObject(); userActionObj.id = spAction.Id.ToString(); userActionObj.name = spAction.Name; userActionObj.descr = spAction.Description; userActionObj.seq = spAction.Sequence; userActionObj.scriptBlock = spAction.ScriptBlock; userActionObj.scriptSrc = spAction.ScriptSrc; lstUserActionObjs.Add(userActionObj); tcout("-----------------------------"); tcout("Found UserCustomAction:"); tcout(" - Id", spAction.Id.ToString()); tcout(" - Name", spAction.Name); tcout(" - Title", spAction.Title); tcout(" - CommandUIExtension", spAction.CommandUIExtension); tcout(" - Description", spAction.Description); tcout(" - Group", spAction.Group); tcout(" - ImageUrl", spAction.ImageUrl); tcout(" - Location", spAction.Location); tcout(" - RegistrationId", spAction.RegistrationId); tcout(" - RegistrationType", spAction.RegistrationType.ToString()); tcout(" - Scope", spAction.Scope.ToString()); tcout(" - ScriptBlock", spAction.ScriptBlock); tcout(" - ScriptSrc", spAction.ScriptSrc); tcout(" - Sequence", spAction.Sequence); tcout(" - Url", spAction.Url); } } } catch (Exception ex) { tcout(" *** ERROR", GetExcMsg(ex)); ErrorOccurred = true; } e.Result = new List <object>() { lstUserActionObjs }; }
/// <summary> /// </summary> private void bgw_LoadActions(object sender, DoWorkEventArgs e) { var lstUserActionObjs = new List <UserActionObject>(); try { var targetSite = new Uri(tbSiteUrl.Text.Trim()); using (ClientContext ctx = new ClientContext(targetSite)) { ctx.Credentials = BuildCreds(); FixCtxForMixedMode(ctx); Site site = ctx.Site; ctx.Load(site, x => x.ServerRelativeUrl); ctx.ExecuteQuery(); tcout("Site loaded", site.ServerRelativeUrl); var spActions = ctx.Site.UserCustomActions; ctx.Load(spActions); ctx.ExecuteQuery(); tcout("UserCustomActions Count", spActions.Count); var spActionsSorted = spActions.ToList <UserCustomAction>().OrderBy(x => x.Sequence).ThenBy(x => x.Name); foreach (UserCustomAction spAction in spActionsSorted) { var userActionObj = new BandR.UserActionObject(); userActionObj.id = spAction.Id.ToString(); userActionObj.name = spAction.Name; userActionObj.descr = spAction.Description; userActionObj.seq = spAction.Sequence; userActionObj.scriptBlock = spAction.ScriptBlock; userActionObj.scriptSrc = spAction.ScriptSrc; lstUserActionObjs.Add(userActionObj); tcout("Found UserCustomAction", userActionObj.id, userActionObj.name, userActionObj.seq); if (!userActionObj.descr.IsNull()) { tcout("Description", userActionObj.descr); } if (!userActionObj.scriptBlock.IsNull()) { tcout("ScriptBlock", userActionObj.scriptBlock); } if (!userActionObj.scriptSrc.IsNull()) { tcout("ScriptSrc", userActionObj.scriptSrc); } } } } catch (Exception ex) { tcout(" *** ERROR", GetExcMsg(ex)); ErrorOccurred = true; } e.Result = new List <object>() { lstUserActionObjs }; }