0

I am developing a visual web part that will do some some heavy operations. It is a visual web part in SharePoint 2013, on premise.

I want to load my data asynch. Ajax toolkit is not compatible and gives problem, as far as I have heard and tried.

Any tips for how my web part can load asynch on this sample code?

This is my code in c#

  protected void Page_Load(object sender, EventArgs e)
    {
        GetCurrentUserData();
    }

    private void GetCurrentUserData()
    {
        try
        {
            var web = SPContext.Current.Web;
            var currentUser = web.CurrentUser.LoginName;
            var serverContext = SPServiceContext.GetContext(web.Site);
            var profileManager = new UserProfileManager(serverContext);
            var currentUserprofile = profileManager.GetUserProfile(currentUser);

            var displayName = currentUserprofile.DisplayName;
            LblName.Text = displayName;

            web.Dispose();
        }
        catch (Exception ex)
        {
            LblError.Text = "Exception.";
        }          
    }

This is my code in asp.net

<%@ 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="WpProfilePropertiesUserControl.ascx.cs" Inherits="XX.MySite.SPI.WebParts.WpProfileProperties.WpProfilePropertiesUserControl" %>

<div>
    <asp:Label ID="LblError" runat="server" Text=""></asp:Label>
    <asp:Label ID="LblName" runat="server"></asp:Label> 
</div>
1
  • 1
    You can use JavaScript client object model. Commented Feb 24, 2015 at 15:12

1 Answer 1

1

I wrote this code here so I can't guarantee it will work as is. It should get you pointed in the right direction.

private Func<string> _func;

protected void Page_Load(object sender, EventArgs e)
{
    _func = new Func<string>(GetCurrentUserData);

    Page.RegisterAsyncTask(
        new PageAsyncTask(
            new BeginEventHandler(BeginGetData),
            new EndEventHandler(EndGetData),
            new EndEventHandler(TimoutGetData),
            null, true));    
}

public IAsyncResult BeginGetData(object sender, EventArgs e, AsyncCallback cb, object state)
{
    return _func.BeginInvoke(cb, state);
}

public void EndGetData(IAsyncResult ar)
{
    var data = _func.EndInvoke(ar);
    LblName.Text = data;
}

public void TimoutGetData(IAsyncResult ar)
{
    LblError.Text = "Timeout.";
}

private string GetCurrentUserData()
{
    try
    {
        var web = SPContext.Current.Web;
        var currentUser = web.CurrentUser.LoginName;
        var serverContext = SPServiceContext.GetContext(web.Site);
        var profileManager = new UserProfileManager(serverContext);
        var currentUserprofile = profileManager.GetUserProfile(currentUser);

        return currentUserprofile.DisplayName;
    }
    catch (Exception ex)
    {
        return ex.Message;
    }          
}

I discuss this pattern at around the 52 minute mark of this recording of Advanced SharePoint Web Part Development. In the video I use a delegate instead of a lambda but the pattern is the same.

5
  • Thanks a lot for the sample code. I am tying to use it, and understand how asynch is working. In the above code I get a null pointer exception on var web = SPContext.Current.Web;, any idea why ?
    – Ilyas
    Commented Feb 24, 2015 at 16:20
  • 1
    Are you using the farm solution or the sandbox solution version of the Visual Web Part template. You can tell by looking in the folder for the Web Part in the solution explorer. If there is a <web part name>.cs and <web part name>.ascx, it's the farm solution version. Commented Feb 24, 2015 at 16:56
  • I am using the farm template.
    – Ilyas
    Commented Feb 24, 2015 at 17:47
  • 1
    Then the SPContext should be populated by the time Page_Load fires. You could try using Page_PreRender instead of Page_Load and see if that works. Commented Feb 24, 2015 at 17:55
  • Hmm. Still get the same. I'll keep poking at it. Thanks a lot for your tips.
    – Ilyas
    Commented Feb 24, 2015 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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