Tuesday, April 24, 2012

Delaying an autocomplete jQuery search

I recently implemented an autocomplete function on a site, using jQuery to update the results and a JsonResult controller to get the actual data.  When the content of a search box changed (using event trigger onkeyup), it would perform a search on the contents of that text box.  I've gone a few steps further and split the text box on spaces and commas, to create a wildcard search, but that's not the point of this post.

The issue I saw is that a new call to the database or cache was being carried out with every keypress.  This was a problem.  So I found some code on http://jsbin.com/ebela4/8/edit#javascript,html and adapted it slightly.  Here it is:


var delay = 600;
var isLoading = false;
var isDirty = false;

function AutoCompleteChange() {
if (!isLoading) {
// This only performs a search if four or more characters have been
// entered. Change the number to change this restriction.
if ($("#txtSearch").val().length >= 3) {
isLoading = true;
setTimeout(function () {
isLoading = false;
if (isDirty) {
isDirty = false;
// Perform actual search
// INSERT YOUR CODE HERE
}
}, delay);
};
};  
};

Thursday, April 19, 2012

SharePoint 2013 - potential screenshots

These are either actual screenshots of SharePoint 2013, or are someone's personal mockups of what SharePoint might look like under the Metro layout.  Either way, looks pretty.

http://www.alexmanchester.com/alexmanchester/2012/04/is-this-how-sharepoint-2013-will-look-sharepoint-metro.html

SharePoint HTML5 master page

Not much use to me in the current situation, as many users are still using browsers that don't support HTML5, but good to know for the future - a HTML5 master page for SharePoint 2010 that looks very shiny indeed.

http://kyleschaeffer.com/sharepoint/v5-responsive-html5-master-page/

Wednesday, April 18, 2012

SharePoint & SAP - getting started

I am beginning work on a contracts management site in SharePoint in the next few months.  Part of the requirements of this site is for contracts to be assigned to particular customers (amongst other things).  It would be very easy for me to implement a simple SharePoint list with a list of customers, but this would be boring, dull and intuitive, as the user would have to add a customer each time.  It would be easy just to grab the customer records from SAP, which we use for SAP-type things.

The team that administrate SAP have been using SAP Business Connector for some years, and (as is the norm, from what I understand) are reluctant to stop using this.  Part of the reason for this is that this team believe they would lose some control over SAP.  However, we have a steadily-growing BizTalk implementation, and to get SAP piped into BizTalk would be a massive selling point not only for future users of SharePoint, but for other applications also.

Here is some quick research that I did on the subject. I imagine there will be more.

Working with SAP Business Connector would seem to require the purchase of additional software such as ERPConnect Services by Theobald Software - http://www.theobald-software.com/en/products/erpconnectservices.htm .  Alternatively, if SAP Business Connector can expose XML, a custom development could be written in .NET, using SharePoint Business Connectivity Services (BCS), but this would require development time.

Working with BizTalk is extremely simple and well-documented - for example, there is a whole series of blog posts on this exact topic here: http://kentweare.blogspot.de/2009/10/sharetalk-integration-sharepointbiztalk.html which was the first site I found when carrying out a search on this.

Essentially, it is possible to communicate with any service or server to any other service or server - everything is possible.  Things just require time and money.  Using BizTalk introduces nothing but benefits, so it's just a case of convincing everyone to accept change.

Expect more posts on this subject as the project continues.  Even with the massive userbase of BizTalk and SharePoint, there does seem to be a bit of a gap when it comes to SAP (with the exception of the above link) and I'm sure I'll be finding things out.

Thursday, April 5, 2012

Parsing XML with : in the name

When parsing XML in C#, problems arise when XML contains a colon (:).  For example, the start of last.fm's resulting XML when performing a search looks like this:


<results xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" for="tsin-tsi">
<opensearch:Query role="request" searchTerms="tsin-tsi" startPage="1"/>
<opensearch:totalResults>43</opensearch:totalResults>
<opensearch:startIndex>0</opensearch:startIndex>
<opensearch:itemsPerPage>30</opensearch:itemsPerPage>

It turns out that these "opensearch" elements are actually part of the opensearch namespace.  It might not seem relevant for a text-based result file like XML, but it is - you have to declare that namespace before you can grab the totalResults, startIndex or itemsPerPage.  To get the number of results, I used this code:

XNamespace opensearch = "http://a9.com/-/spec/opensearch/1.1/";
XElement pagecount = doc.Descendants(opensearch + "totalResults").FirstOrDefault();
numberofResults = Convert.ToInt32(pagecount.Value);


Wednesday, April 4, 2012

Passing Display Language from SharePoint 2010 to a Silverlight app

This is related to cultures, my current obsession.  Running a Silverlight app in a browser will allow you to change the browser language and have the Silverlight app respond appropriately.

SharePoint 2010 has a "Display Language" option for each user - as long as the appropriate SharePoint 2010 language pack has been installed, the SharePoint site will be changed to show translations that have been assigned for that language.  However, as the browser language hasn't changed, Silverlight web parts don't reflect that change.

The solution?  Create a new Silverlight web part control that inherits from System.Web.UI.WebControls.WebParts.WebPart, and in the Render section (that outputs to a supplied HtmlTextWriter called writer) -

            string s = String.Format(@"              
                <div id='SilverlightObjectDiv_{0}' style='display: block; overflow:hidden'>
                    <object class='ms-dlgDisable' id='SilverlightObjectTag_{0}' width='100%' height='{2}'
                        data='data:application/x-silverlight-2,'
                        type='application/x-silverlight-2' >
                        <param name='source' value='{1}'/>
                        <param name='initParams' value='MS.SP.url={3},culture={4}'/>
                        <param name='culture' value='{4}' />
                        <param name='uiculture' value='{4}' />
                        <!-- Display installation image. -->
                        <a href='http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0'
                            style='text-decoration: none;'>
                            <img src='http://go.microsoft.com/fwlink/?LinkId=108181'
                                alt='Get Microsoft Silverlight'
                                style='border-style: none'/>
                        </a>
                    </object>
               <iframe id='_sl_historyFrame' style='border-bottom: 0px; border-left: 0px; width: 0px; display: block; height: 0px; visibility: hidden; border-top: 0px; border-right: 0px'></iframe>&#160;
                </div>      
                ",
                 this.ClientID.ToString(),
                 _xapFileUrl,
                 (this.Height.Value - 20).ToString(),
                 SPContext.Current.Web.Url, System.Threading.Thread.CurrentThread.CurrentUICulture.ToString()                
                 );


            writer.Write(s);

The important thing to note here is the "culture" and "uiculture" params - System.Threading.Thread.CurrentThread.CurrentUICulture would normally pass the browser language but when running as a SharePoint web part, seems to pass the chosen display language instead.

Tuesday, April 3, 2012

Silverlight Localization

Two useful links for localizing Silverlight applications:

http://www.michaelsnow.com/2010/04/28/silverlight-tip-of-the-day-7-localized-resources/ walks you through the process of making your app culture-observant.

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/10/27/10864.aspx enables you to use your browser language settings to determine the culture automatically.