08 April, 2009

Sharepoint Access Exception for elevated privileges

Working with FBA I encountered the most bazaar issue. I want my FBA user that is not Admin to be able to update their profile details both their name and email address. SO this works perfectly well when I want to update the DB but the issue comes when I want to update the SPUser account of that user.

I tried many different solutions, Allowing Anonymous Access on both the web and the site and also impersonating the user. In the end the solution which is not that obvious was to disable the FormDigest. Found in this msdn thread.

Here is my final code solution : Prior to this i make sure that this code only executes when the user is non admin

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(this.Site.ID))
{

site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb())

{
SPWebApplication webApp = web.Site.WebApplication;
bool formDigest = webApp.FormDigestSettings.Enabled;
webApp.FormDigestSettings.Enabled = false;


web.AllowUnsafeUpdates = true;
SPIisSettings usersettings = Utils.GetFBAIisSettings(site);
SPUser spUpdateuser = web.AllUsers[usersettings.MembershipProvider + ":" + userName];
if (spuser != null)
{
spUpdateuser.Email = txtEmail.Text;
spUpdateuser.Name = txtFullName.Text;
spUpdateuser.Update();
}
webApp.FormDigestSettings.Enabled = formDigest;
}
}
}
);

15 January, 2009

jQuery hide unchecked Check Boxes

In my scenario a user can select people to add to a list (using checkboxes). The update is done via jQuery $post so there is no refresh. In order to make it seem more fluent I need to hide all the people that are not checked making it seem as though there was a full update. *a full update happens when the user refreshes the page.

So the solution: use jQuery to hide checkboxes that are not checked.


var divId = "yourDivID";

$("#"+divId+" input:checkbox").not(":checked").each(function(){
$(this).next().hide();
$(this).hide();
});


the text next to the checkbox is inside a span tag. This allows it to be recognised correctly when using .next()

jQuery Check Boxes all Checked Values from Div

Something super awesome with jQuery. The ability to get all the values of checkboxes that are checked within a specific div.

//divid is your unique DIV id
$("#divId input:checked").each(function(){
alert($(this).val());
});

As simple as that! We get all the checked ckeckbxes from divId and then alert each of their values. Just remember to leave a space between the divId and input.
Enjoy