Last active
August 29, 2015 14:08
-
-
Save neremin/ed7aa9450bd4dd1a1027 to your computer and use it in GitHub Desktop.
Intercepting SharePoint's SaveButton action to decorate/replace saving logic. Tested on SP 2013 Foundation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// On your page with SaveButton you could do the following trick | |
// (in my case save button is added in DataFormWebPart's XSL markup): | |
SPContext itemContext; | |
DataFormWebPart dataForm; // from designer's code behind | |
void Page_Init(object sender, EventArgs e) | |
{ | |
// NOTE: by some reason ItemContexts of controls in DFWP are differ, | |
// so only SaveButton's OnSaveHandler is invoked | |
itemContext = dataForm.Controls | |
.FindControlRecursive<SaveButton>().ItemContext; | |
} | |
void Page_Load(object sender, EventArgs e) | |
{ | |
if (itemContext.FormContext.FormMode == SPControlMode.New || | |
itemContext.FormContext.FormMode == SPControlMode.Edit) | |
{ | |
itemContext.FormContext.OnSaveHandler += OnSaveHandler; | |
} | |
} | |
void OnSaveHandler(object sender, EventArgs eventArgs) | |
{ | |
// TODO: Add your code before saving the item | |
SaveButton.SaveItem(saveButton.ItemContext, false, string.Empty); | |
// TODO: Add your code after saving the item | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class WebControlExtensions | |
{ | |
public static TControl FindControlRecursive<TControl> | |
( | |
this ControlCollection controls | |
) where TControl : Control | |
{ | |
if (controls != null) | |
{ | |
foreach (Control control in controls) | |
{ | |
var foundControl = control as TControl | |
?? control.Controls.FindControlRecursive(); | |
if (foundControl != null) | |
{ | |
return foundControl; | |
} | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class WebControlExtensionsTests | |
{ | |
[Test] | |
public void FindControlRecursive_returns_first_sibling_by_default() | |
{ | |
var root = new Control(); | |
var expected = new Button(); | |
root.Controls.Add(new Control()); | |
root.Controls.Add(expected); | |
root.Controls.Add(new Button()); | |
root.Controls.Add(new Control()); | |
root.Controls.FindControlRecursive<Button>() | |
.Should().BeSameAs(expected); | |
} | |
[Test] | |
public void FindControlRecursive_returns_nested_before_sibling_by_default() | |
{ | |
var root = new Control(); | |
var expected = new Button(); | |
root.Controls.Add(new Control()); | |
root.Controls[0].Controls.Add(expected); | |
root.Controls.Add(new Button()); | |
root.Controls.Add(new Control()); | |
root.Controls.FindControlRecursive<Button>() | |
.Should().Be(expected); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment