Monday, April 16, 2012

Another Reason for 'Group can not be found' error in SharePoint 2010



Recently I was working on an workflow where I was accessing a group from SharePoint 2010 and from there trying to get user's mail ids. I created a group in SharePoint 2010 from UI. Since this group was supposed to hold only users (approvers for workflow), I did not assign any permission to the group. I left all the check boxes unchecked.

Next when you try to get the group from code in workflow, you will get error saying 'Group can not be found'. Below is the code I had written in my VS 2010 workflow.

SPGroup spgApprovers = workflowProperties.Site.RootWeb.Groups["Some Group"];

After some googling, I found that if you have not assigned any permission to the group, it will not show up in the code. So I went back to the group, deleted it and recreated with reader permission.

Hope this will help someone...
Happy SharePointing...

-Vighnesh Bendre

Friday, April 13, 2012

Validation before Save in New or Edit form



If you want to do validation based on other column in your out of the box New or Edit form, you can do it using JQuery in SharePoint 2010.

In the code below I am trying to validate a text column if the value in the status drop down list is ‘In Process’.

The function PreSaveAction() gets fired when the user clicks on ‘Save’ button in the New or Edit form. In this function you can create object of the drop down list and text control and check if the text control is empty when the status is ‘In Process’.

function PreSaveAction()
{
   var selectedVal = $("select[title='Status']").find('option:selected').text();
   if(selectedVal == 'In Process')
   {
       var chrgtoNum = $("input[title='SID Number']").val();
       if(chrgtoNum == '')
       {
          alert('Please enter SID Number.');
          return false;
       }
       else{return true;}
   }
   else {return true;}
}


You can see my other blog to learn how to put this code in New and Edit form.
Validation or control form from JQuery - Default New and Edit forms


Happy SharePointing...

-Vighnesh

Validation or control form from JQuery - Default New and Edit forms



When you create a custom list, you get default edit and new forms with it. So when you are entering data or modifying it SharePoint provides you a form in a popup that enables you to enter or modify data.

But then, if you want to add custom features to the new or edit form, you can do so using JQuery. In the below example, based on the valued selected in the dropdown list, I want to enable or disable other control. And I want to add this JQuery code to the New and Edit forms of the custom list.

function HideColumn(stat, active) 
{  
        var activeObj = $("input[title='" + active + "']");
        
       //initially when the user opens the form, we need to check if the user has selected the status that we have written code for…
       //if selected value is other than Suspend, disable the control
        var initialVal = $("select[title='"+ stat +"']").find('option:selected').text();
        if(initialVal == 'Suspend')
        {$(activeObj).attr('disabled', false)}
        else
        {$(activeObj).attr('disabled', true)}

         //Now handle dropdown list’s change event. In this event we will check what is the selected value of the ddl, based on the value, we enable or disable the control…
 $("select[title='"+ stat +"']").change(function() 
 { 

  var selectedValue = $("select[title='"+ stat +"']").find('option:selected').text();
 
         if(selectedValue == 'Suspend') 
  {
              $(activeObj).attr('disabled', false)
          }
  else {
       $(activeObj).attr('disabled', true)
              }
 });
}

$(document).ready(function() {HideColumn('Status', ' Active');} ); 

In the above example, I have written a function HideColumn which will be called when the document is ready. The parameters to this function are the column names of dropdown list – ‘Status’ and other control which we want to enable/disable.

Now when the user opens the form in edit mode, the status ddl is already selected. At this time, we want to make sure based on the selected value, we change the state (enable/disable) of the other control. In SharePoint you are not aware of the ‘Id’ property of the control. Or this property will be having some junk value. But what you can depend on is the ‘Title’ property. So by passing the title property, we can create the control object.

var activeObj = $("input[title='" + active + "']");

You can get the selected text of the drop down list by using the find method as below.

var initialVal = $("select[title='"+ stat +"']").find('option:selected').text();

Once you are done with enabling and disabling the control at start up, you have to attach change event handler to the status drop down list.

$("select[title='"+ stat +"']").change(function(){});

This function will fire every time you change the value in the drop down list. Now based on the selected value, you can enable or disable the other control.


Now, how do you put this on to the new and edit form? Well go to the list tab, click on ‘Form Web Parts’ and select ‘Default New form’. In the form, click on add web part, click on ‘Media and Content’. Add ‘Content Editor’ web part.



Now in the ‘Editing Tools’ – ‘Format Text’ tab, click on ‘HTML’ – ‘Edit HTML Source’. Paste you JQuery code in this popup dialog. Don’t forget to set the chrome type to ‘None’ in the web part properties.

Follow the same steps to configure JQuery code in the edit form also.

Happy SharePointing...

-Vighnesh