2

I have people picker in my custom web part (SP 2013), and all my controls in updatePanel. Requirement is to set a custom error message for people picker. I am able to change the error message in button click event(refer code snippet) and I am able to do same in jquery too. But how do i do that when user clicks on "Check names"? I know it's an anchor tag with title "Check Names".

I prefer to have this functionality in code behind rather than using JQuery.

below is, what i did

Btn_Click()
{
 if(pplEditorId.IsValid)
 {
   pplEditorId.Errormessage="Invalid user id";
 }
}
1

PeopleEditor control is doing a callback to the page itself when you press "Check names" button.

In order to inject your own custom error message when "Check names" button validation resolves to false you need to inherit from PeopleEditor control:

using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Xml;

namespace PeoplePickerCustomError
{
    public class CustomPeopleEditor : PeopleEditor, ICallbackEventHandler
    {
        private string CUSTOM_ERROR_MESSAGE = "My custom error message!";

        public new string GetCallbackResult()
        {
            XmlDocument xmlDoc = new XmlDocument();
            string originalResponse = base.GetCallbackResult();
            xmlDoc.LoadXml(originalResponse);

            // if there's no error return original response
            if (string.IsNullOrEmpty(xmlDoc.DocumentElement.Attributes["Error"].Value))
            {
                return originalResponse;
            }

            xmlDoc.DocumentElement.SetAttribute("Error", CUSTOM_ERROR_MESSAGE);
            string customResponse = xmlDoc.OuterXml;

            return customResponse;
        }

        public new void RaiseCallbackEvent(string eventArgument)
        {
            base.RaiseCallbackEvent(eventArgument);
        }
    }
}

Then just reference it in your webpart:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PeoplePickerHolderUserControl.ascx.cs" Inherits="PeoplePickerCustomError.PeoplePickerHolder.PeoplePickerHolderUserControl" %>
<%@ Register TagPrefix="custom" Namespace="PeoplePickerCustomError" Assembly="PeoplePickerCustomError, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c6a38d5a688a5dd0" %>

<custom:CustomPeopleEditor runat="server" ID="pePeoplePicker" />

(change the assembly information in Register tag to match your own)

0

you might want to try something like this

  <%@PageLanguage="C#"%><%@RegisterTagprefix="wssawc"Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%><%@RegisterTagprefix="SharePoint"Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%><scriptrunat="server">

  protected void btnCheck_Click(object sender,EventArgs e)
  {
    bool IsValid = this.userPicker.IsValid;
    if(!IsValid)
    {
      this.userPicker.ErrorMessage = "Invalid user id";
    }

  }


  </script><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><div><wssawc:PeopleEditorAllowEmpty=falseValidatorEnabled="true"id="userPicker"runat="server"ShowCreateButtonInActiveDirectoryAccountCreationMode=trueSelectionSet="User,SecGroup,SPGroup"IsValid=/></div><asp:ButtonID="btnCheck"runat="server"Text="Button"OnClick="btnCheck_Click"/></form></body></html>
  • 1
    whats different in this answer? did you read my question? – Ahmed Nov 10 '14 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.