Pages

Monday, August 8, 2011

Read only field in SharePoint EditForm.aspx via Javascript


When we need to make some fields as read only when users wants to edit the item.This thing we can do very easily by SharePoint Designer , open that edit form and create one custom form and edit the field in which we want to make as read only we can change displaymode as edit. But what if do not want to change by SharePoint Designer.
We have very easy way which named as “JavaScript”.
I need to make one field as read-only, here is sample code
Open the editform.aspx page for a list and add one “Content Editor Webpart”, add the below javascript code.
<script type="text/javascript">

function MakeReadOnly()
{
// find all the elements with tag Name as INPUT
var elements=document.body.getElementsByTagName("INPUT");
// loop through all the elements till we find an element with type text and title as name of our field
for (index=0; index < elements.length;++index)
{
if(elements[index].type=="text")
{
if(elements[index].title=="Address") //Field name
{
elements[index].readOnly=true;
}
}
}
}
_spBodyOnLoadFunctionNames.push("MakeReadOnly()");</script>

Hope this helps!!!
Disha Shah

Monday, August 1, 2011

SharePoint : Validate controls before the item is saved via PreSaveAction() in SharePoint 2007/2010

I do have one requirement in which I need to check if they check one of the checkbox then they must have to enter the value. Without eventhandler or any custom code how could we do that?
We can achieve this thing via Content Editor WebPart and write JavaScript inside that. PreSaveAction() is manly used to validate controls before the item is saved( new item or edit).
How to do that?
Let us edit the page and add one content editor webpart , and add the javascript code in that content editor webpart and save page.
Sample of Javascript Code.
<script type="text/javascript">
function PreSaveAction()
{
var control = getTagFromIdentifierAndTitle("input","BooleanField","Other available");
var control1 = getTagFromIdentifierAndTitle("input","TextField","Other Value");
if(control.checked)
{
var StrToCheck= control1.value.replace(/^\s+|\s+$/, '');
//then we check for the length of the string if its 0 or not
if( StrToCheck.length==0 || StrToCheck =='0')
{
alert('Enter the value for Other');
}
}
else
{
control1.value=0;
}
return false;
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);
  for (var i=0; i < tags.length; i++) {
    var tempString = tags[i].id;
    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
    }
  }
  return null;
}</script>

Hope this helps!!!
Disha Shah

Monday, July 25, 2011

SharePoint 2010 Membership Error :“The method or operation is not implemented”

Hello Friends
I was working with “Claims based authentication” in SharePoint 2010, develop registration page for new user so first I need to check whether that user already exists or not so when I checked for GetUser of Membership it always  throws error
I got this error: “The method or operation is not implemented”.
Nothing about my membership provider was custom, it was all default .Net stuff. I also found this to happen on other methods provided by the Membership object.
We can find out from here
MembershipProvider _provider = Membership.Providers["MyCustomMembershipProvider"];
MembershipUser user = _provider.GetUser(“username”, true);

if (user == null)
{
}
 ["MyCustomMembershipProvider "] is provider name in web config file

Hope this helps!!!
Disha Shah