Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Sunday, June 5, 2011

CrossListQueryInfo and CrossListQueryCache gotchas

CrossListQueryInfo class which comes with the Microsoft Publishing infrastructure makes use of object caching techniques to query the requested data from the cache instead from the database. Remember to use this facility only if your code runs against a MOSS server and not on a WSS server. As this makes use of  object cache stored in WFE servers, it avoids to database input/output and network latency between the WFE's and database server.

Although it uses SPSiteDataQuery internally,the very first request using CrossListQueryCache takes more time than SPSiteDataQuery. Practically it includes the time for querying, caching and returning the values. But subsequent queries fetch results in a flash and no where comparable with SPSiteDataQuery.

Interesting fact is that this CrossListQueryCache and QueryInfo will not work in a Console line tool, it badly depends on the context. If you are really trying this interesting query technique in a web part, whenever you implement any change in your webpart and trying to debug. Apart from doing the IISReset/App pool recycle, don't forget to flush the object cache

A sample CrossListQueryCache
public DataTable GetDataTableUsingCrosslistQuery(SPSite site)
{
CrossListQueryInfo clquery = new CrossListQueryInfo();
clquery.RowLimit = 100;
clquery.WebUrl = site.ServerRelativeUrl;
clquery.UseCache = true ;
clquery.Lists = "";
clquery.Webs = "";
//clquery.Query = "";
clquery.Query = "569";
clquery.ViewFields = "";
CrossListQueryCache cache = new CrossListQueryCache(clquery);
DataTable results = cache.GetSiteData(site);
return results; 
}

Tuesday, September 28, 2010

Show communicator status in your custom application pages

When ever we show a username in a custom application page or in a custom web part, adding that small presence aware icon before the user name will be a value addition to the end users which allows to see the person's availability.

An ActiveX control called NameCtrl in Name.Dll is responsible to retrieve the user status and rendering.

Use the following snippet, get the string and render it on your custom page to make the presence aware work for you. Ensure Ows.Js is referenced in your master page, this javascript is responsible for instantiating the ActiveX control and rendering.

public static string GetIMStatusForUser(string personEmail, string personName)
{
string statusJS = String.Format("<span><img border=\"0\" height=\"12\" width=\"12\" src=\"/_layouts/images/blank.gif\" onload=\"IMNRC('{0}')\" id=\"IMID\"
ShowOfflinePawn=1><a href=\"mailto:{0}\">{1}</a></span>",
personEmail,personName);
return statusJS
}

Thursday, August 5, 2010

SharePoint 2010 SPSite : System.IO.FileNotFoundException

Can any one spot an error in the following snippet. Following snippet tries to open a SPSite, SPWeb and tries to open a List to get it item count. All objects exist in the server and this code was built with VS 2010 for SharePoint 2010 in x86 Debug mode.

I ended up with the following exception

System.IO.FileNotFoundException was unhandled
Message=The Web application at http://Foo could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
Source=Microsoft.SharePoint
StackTrace:
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Administrator\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


After a couple of minutes I got things straight. There was a mistake in the build, as a basic rule while developing for SharePoint 2010 all your build should be in x64 mode not in x86 mode.

Basic mistake, but it hurts and the error message is not really helping...

Thursday, August 27, 2009

Including quotes in verbatim strings

As we all know C# supports two types of strings: regular and verbatim strings. Escape sequence character such as backslash is not processed within this verbatim string sequence except quote-escape-sequence("). I recently stumbled across this C# basic string rule


So to encode a xml fragment like this

string s="[formbody xmlns="http://www.Foo.com/FormBody"][/formbody]"

We should have to write as follows using verbatim character and Quote escape sequence

string s = @"[FormBody xmlns=""http://www.Foo.com/FormBody""][/FormBody]";

but C# handles this quote-escape-character as regular escape sequence internally


So beware while using verbatim strings and escape sequences altogether especially in cases of handling XML/HTML strings in C# code

For more Check the C# language Specification


Thursday, November 20, 2008

Filter SharePoint List items based on User context

Filtering the items created by the logged in user can be done by simply modifying the existing View or by adding a new view which has "Created By" value = [Me].

But how to achieve the same thing in Code, Getting a list looping it and filtering the result will do. But sure performance will go down.

Here comes the CAML query for the rescue of SharePoint developer. CAML is having a token which automatically uses the current user context.

So we can write a code snippet as follows to get items created by the current user.



SPList sList=[Get the list to query here];
SPQuery query = new SPQuery();
query.Query = string.Format(
"[Where]"+
+ "[Eq]"
+ "[FieldRef Name='Author' LookupID='TRUE'/]"
+ "[Value Type='User'][UserID /][/Value]"
+ "[/Eq]"
+ "[/Where]");
SPListItemCollection listItems = sList.GetItems(query);


you will get the list items only created by the current user, no iteration, no filteration happens only clean CAML query.

Replace all square brackets[ ] with angle brackets < >. Blogger refused to accept the angle brackets.

Happy coding,