Thursday, 25 June 2015

Nintex Forms show loading dialogue for required time

One of our Nintex Form has complex Javascript which queries web services and loads meta data. For this operation, the javascript runs for few seconds and we want user not
  1.  To start typing on any field of form or accessing the form before it loads all meta data
  2.  To complain that form is incomplete before it loads all meta data
For this we can add below Javascript to show loading animation to user so that user can wait without complaining. It gives better usability.

$(window).load(function () {

waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose('   ', 'Please wait...', 110, 280);
   
setTimeout(function () {  
waitDialog.close();
    }, 5000);
});

JQuery has to be referenced in Nintex Form. It uses SharePoint's native modal dialog.

Nintex form is blank in browser

If Nintex form is not getting rendered, that is if browse shows white page instead of Nintex form, check for any javascript error using browser debugger.

Get Current Users Display name in ASP.Net Web application

To Get current user's display name without LDAP settings.


  public string HelloUser()
        {
            string dispName = "";
            Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
           
            using (System.Web.Hosting.HostingEnvironment.Impersonate())
            {
                WindowsPrincipal principal = (WindowsPrincipal)User;
                PrincipalContext pc = new PrincipalContext(ContextType.Domain); ;
                UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
                dispName = up.DisplayName;
                
            }            
            return "Hello - " + dispName + ", your login is " + HttpContext.Current.User.Identity.Name;
        }

Note: Make sure windows impersonation is turned on in IIS.

Saturday, 20 June 2015

SharePoint 2010 Visual WebPart gives 403 error to users

We have a Visual Webpart deployed to SharePoint 2010 with following key functionality
  1. Access SQL DB
  2. Access SP Groups to see if current user is part of few SP groups
This Web Part uses AJAX control toolkit.
Our SharePoint farms are scheduled to recycle App Pools over night.

Problem:
In the morning when a normal user access the page containing Visual Web Part, IE will show 403 error.
Once Site admin access the page the error will go away and normal users can access the page without any problem.

Solution:
·         Give Read permission for Everyone to \bin and \_app_bin folders
·         Copy AJAX Control toolkit dll to GAC
·         Add Authenticated users to group WSS_WPG

Friday, 19 June 2015

Prevent users from accessing All Items page of SharePoint 2010

In our SharePoint portal we show users only Nintex Forms (can be accessed by links in emails sent by Workflows) and few web part pages restricting access to default SharePoint pages like AllItems.aspx.

To achieve this 
1. Upload 
             jquery-1.8.3.min.js   and
             jquery.SPServices-0.7.2.min.js

to Site Assets library

2. Edit AllItems.aspx page (or any page you want to restrict access to normal users) and add below script. (Even if the user try to access the page with link, they will be redirected to home page).

<script type="text/javascript" src="/SiteAssets/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='SiteAdmins']").length != 1) {
                                location.href = $().SPServices.SPGetCurrentSite();
                                 }
                      }
       });
});
</script>

After adding above script, only members of SiteAdmins can access that page, all other user will be redirected to home page.

Show 'Site Actions' ribbon menu only for SP Group members in SharePoint 2010

Step 1:
Create a SharePoint group 'SiteAdmins' at Site Collection level with Design and Contribute access

Step 2:
Add\Upload below 2 JS files to SiteAssets of root site
  1. jquery.SPServices-0.7.2.min.js
  2. jquery-1.8.3.min.js
Step 3: 
Refer above 2 JS in Master file.
Edit default Master page, find ‘<asp:ScriptManager’ under Form tag and replace(or include the script references) to include JQuery and SPServices scripts

 <asp:ScriptManager id="ScriptManager" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true" >
  <Scripts>                             
                                <asp:ScriptReference Path="<%$SPUrl:~SiteCollection/SiteAssets/jquery-1.8.3.min.js%>"></asp:ScriptReference>                         
                                <asp:ScriptReference Path="<%$SPUrl:~SiteCollection/SiteAssets/jquery.SPServices-0.7.2.min.js%>"></asp:ScriptReference>                               
                </Scripts>             
</asp:ScriptManager>

Step 4:
Hide ‘Site Actions’ and top ribbon for all users by default.
<!--<div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle">-->
<div id="s4-ribbonrow" style="display: none;">

Step 5:
Include below script block before between </Body> and </HTML> to check if current user is member of 'SiteAdmins' group and display if yes.

<script type="text/javascript">
$(document).ready(function() {$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='SiteAdmins']").length == 1) {
$('#s4-ribbonrow').show();
}
      }
   });  
   });
</script>

Step 6 (Optional): If you want to display 'Welcome <current user>',  add wssuc:Welcome control (copy & paste) to some other location other than ribbon. 

Thats all, the Ribbon will be hidden for all users except for 'SiteAdmins' members. They have access to 'Site Actions' menu.

Thursday, 29 March 2012

ASP.NET Ajax client-side framework failed to load And\Or 'Sys' is undefined

My ASP.NET application with AJAX controls suddenly started giving script errors in IE8, but it worked perfectly alright in Chrome and Firefox.

Microsoft JScript runtime error: ASP.NET Ajax client-side framework failed to load.
Microsoft JScript runtime error: 'Sys' is undefined


The error was as below in VS.


 In IE8, you can notice the error at left corner of status bar.

Spent 2 days, tried all different option including

a) Modifying web.config
b) Enabling Javascript in IE
c) Restoring IE default settings
d) Removing and adding AjaxControlToolkit.dll in VS2008.
e) Adding axd extension in IIS.

But no luck. I even created new web application with one page and one AJAX control, the same error appeared when i hit F5 in VS2008. But i browse the page in Chrome or Firefox it worked like charm. That concluded me there was nothing wrong in my app.
Exisiting applications hosted in different server showed script error in my IE8 window but worked on others machine.

Atlast, found the solution at http://stackoverflow.com/a/8522460, thanks to Chris Hammond.

Solution is, this could be a caching/compression issue and by putting in the following into Web.Config, resolves the issue.

  <system.web.extensions>
    <scripting>
      <scriptResourceHandler enableCaching="false" enableCompression="false" />
    </scripting>
  </system.web.extensions>