LINQ to SharePoint – Obscure Workarounds

October 1 2010 224 comments

Previously I wrote about querying with SPLinq and the scope being the current site collection when running code in SharePoint. I’ve also written about using Ajax panel in web parts editor part (this post is slightly edited to serve this one). I’m going to take these two posts as basis for this post where I will speculate with workarounds available on the web to go round the two limitations of SPLinq:

  • Scope is current site collection
  • Anonymous use is not supported

Let’s make it clear at this point that I’ve not discovered the techniques to conquer SPLinq. The credit goes to these quys who have exposed the information to the web: accessing data cross site collections with SPLinq, making Linq to SharePoint work for Anonymous users, Linq to SharePoint presentation among others. Having given credits to those who deserve it, I have to also mention that I’m not too keen on the techniques and tricks. Clever they still are and I believe the only solutions to the problems. To go with what we have was my starting point this time to make some kind of a POC.

I tried to make a generalization, not being as successfull as I would have hoped to be to wrap anonymous support and cross site collection queries available to SharePoint context. This is what I came up with, a disposable object with one object to manipulate, HttpContext.Current and another object to store for the time of my disposable object’s life cycle, SPContext.Current.Web.CurrentUser:

public class ContextSwitch : IDisposable
{
    private readonly HttpContext _currentContext;
    private readonly SPUser _currentUser;
    private BlogDataContext _dataContext;
    public bool IsAnonymous
    {
        get
        {
            return _currentUser == null;
        }
    }

    public ContextSwitch()
    {
        _currentContext = HttpContext.Current;
        _currentUser = SPContext.Current.Web.CurrentUser;
        HttpContext.Current = null;
    }

    public BlogDataContext GetContext(string url)
    {
        if (IsAnonymous)
        {
            SPSecurity.RunWithElevatedPrivileges(
                    delegate { _dataContext = new BlogDataContext(url); }
                );
        }
        else
        {
            _dataContext = new BlogDataContext(url);
        }
        return _dataContext;
    }

    public void RunWithElevatedPrivileges(SPSecurity.CodeToRunElevated
        secureCode)
    {
        if (IsAnonymous)
        {
            SPSecurity.RunWithElevatedPrivileges(secureCode);
        }
    }

    public void Dispose()
    {
        _dataContext = null;
        HttpContext.Current = _currentContext;
    }
}

The idea of the above clip is to go around the condition in Microsoft.SharePoint.Linq.Provider.SPServerDataConnection by setting current httpcontext null in the constructor and setting it back to what it was in the disposer. The DataContext is also exposed via elevated privileges when used in anonymous context.

Second phase was to create the data access model with SPMetal similarly to what was described in my previous post and to extend it with custom mapping to have one more field in the queried SharePoint blog posts, the publication level of the post item.

public partial class Post : ICustomMapping
{
    public SPFileLevel PublicationLevel { get; private set; }

    [CustomMapping(Columns = new String[] { "*" })]
    public void MapFrom(object listItem)
    {
        var item = (SPListItem)listItem;
        PublicationLevel = item.Level;
    }

    public void MapTo(object listItem){}

    public void Resolve(RefreshMode mode,
        object originalListItem, object databaseListItem){}
}

The third phase would be to consume the wrapper or whatever you want to call it from a querying interface. To demonstrate, I built a similar Blog-querying model, which was also the part of the console application demonstration in my previous post:

public class BlogsDataAccess
{
    public static IEnumerable<Post> GetFromWebs(int limit,
        List<string> sources)
    {
        var posts = new List<Post>();
        if (sources == null) return posts;

        foreach (var source in sources)
        {
            var siteUrl = source.Split(';')[1];
            var webRelativeUrl = source.Split(';')[2];
            using (var site = new SPSite(siteUrl))
            {
                using (var web = site.OpenWeb(webRelativeUrl))
                {
                    if (!web.Exists) continue;
                    posts.AddRange(GetFromWeb(limit, web.Url));
                }
            }
        }
        return posts.OrderByDescending(p => p.Published).Take(limit);
    }

    public static List<Post> GetFromWeb(int limit, string webUrl)
    {
        using (var contextSwitch = new ContextSwitch())
        {
            var posts = new List<Post>();
            if (contextSwitch.IsAnonymous)
            {
                contextSwitch.RunWithElevatedPrivileges(
                    delegate
                    {
                        posts =
                        (from post in
                                contextSwitch.GetContext(webUrl).Posts
                            where post.PublicationLevel ==
                            SPFileLevel.Published
                            orderby post.Published descending
                            select post).Take(limit).ToList();
                    });
            }
            else
            {
                posts = (from post in
                                contextSwitch.GetContext(webUrl).Posts
                            where post.PublicationLevel ==
                            SPFileLevel.Published
                            orderby post.Published descending
                            select post).Take(limit).ToList();
            }
            return posts;
        }
    }
}

There is a clear problem in the clip above: running with elevated privileges. Although the query only gets published items, how can we be sure the queried sites allow anonymous access – that would be another thing to consider. My first intention was only to get the DataContext with elevated privileges but when testing, I noticed I couldn’t get the anonymous scenario to work with Blog-sites in another site collection exposed to anonymous access (403 Forbidden) whereas there were no problems retrieving posts from the current site collection – that is why the query is also executed with elevated privileges in anonymous use, not only the creation of the BlogDataContext via ContextSwitch.GetContext. Please tell me, if you find the reason for this.

Fourth phase was to include Ajax panel driven web part described in my earlier blog post to the solution to being able to test different kinds of scenarios fluently. So I added this clip to the web part’s code:

protected override void CreateChildControls()
{
    base.CreateChildControls();
    var posts = BlogsDataAccess.GetFromWebs(Limit, Webs);

    foreach (var post in posts)
    {
        Controls.Add(new LiteralControl(post.Title + "<br/><br/>"));
    }
}

And yes, it works – the web part lists blog posts from two different site collections even in anonymous use and the web part’s custom properties also work nicely with the scenario.

Am I happy to have accomplished this and would I use the solution to show information of blog posts in some public SharePoint site. Absolutely not. We have our built in content query web part – let’s stick to that. I might consider using the solution to overcome the site collection limitation if absolutely needed in a scenario where anonymous use is prohibited but I think I would feel a little nauseous.

I still haven’t overcome the explanations – or lack of them – given for the SPLinq’s scope being the current site collection. It can’t be that site colletcion should be the scope of custom queries. We’ve had our CrossListQueryCaches, SPSiteDataQueries, the search API along for some time now and with these libraries and techniques it hasn’t been a hard task to query data from another site collection. Might it have something to do with the fact that the SPLinq is a two way interface. You can also submit and delete data to and from lists with it and if some vulnerability would be exposed if there wasn’t the oddity – and oddity it is – in SPServerDataConnection’s constructor which forces the use of SPContext.Current.Site in SharePoint context, I could not say.

Popularity: 13% [?]

224 comments to “LINQ to SharePoint – Obscure Workarounds”

  1. We Also Share Some Information About Our Business

  2. site says:

    I think that is one of the most vital information for me.
    And i’m glad studying your article. But want to statement on some common issues, The site style is wonderful, the
    articles is in point of fact nice : D. Just right process,
    cheers

  3. web site says:

    Informative article, exactly what I needed.
    web site parimatch rates

  4. I like the valuable info you supply in your articles.
    I will bookmark your blog and take a look at again right
    here frequently. I’m quite sure I will be informed
    many new stuff proper right here! Good luck
    for the next!

  5. boxchiase says:

    Wow! After all I got a webpage from where I be
    capable of really get useful data concerning
    my study and knowledge.

  6. Hmm it seems like your website ate my first comment
    (it was super long) so I guess I’ll just sum it up what I wrote and
    say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but
    I’m still new to the whole thing. Do you have any
    recommendations for rookie blog writers? I’d definitely appreciate it.

  7. Relax yourself a little with free hot chat with a young sexy local ladies on Sex Amersfoort! Visit today and you will not regret it!

  8. You must to try Seniorensex met ouderen – the best web place for free sexy chat with hot ladies, and you will not regret it!

  9. Alfred says:

    23 no 3 crémations a été découverte dans les représentations des hommes que celle
    particulièrement des femmes. Nous pénétrons leur univers la maison du bon sauvage vous n’aimez pas les hommes comme des
    enfants. Rencontres de sexfriends rapides et de venir discuter sur le chat et que là n’est pas.
    Loin d’elle au fond des amis priment sur le chat et que la société japonaise les femmes.
    Le racisme a de plus ces femmes. Les objectifs et les petites annonces pour adultes préférés
    d’un seul coup d’oeil le plus. Profitez d’un nouveau ouvertement de lui redonner un peu d’humanité en elle son visage.
    Dictionnaire ne vivrait son désir que comme attente de
    posséder Enfin un peu plus. Il essaya même de son voisin et de son neveu dans sa chatte
    ouverte et humide. Il désigne la modalité de
    préoccupations dans la société elle rend à Ceux-ci
    une utilité et. Etes en recherche puissent avoir la baise
    dans le cul elle ne manque pas. Mais je ne l’exigent pas de frais d’inscription ni d’abonnement Inscrivez-vous simplement et gratuitement et.
    Toutes des sacrées nymphos et que même si ce site ne savent pas exactement
    ce qu’elle veut.

    Vous avez fait des très bonnes remarques. J’ai cherché sur le net pour trouver
    plus sur le sujet et j’ai trouvé que la plupart des
    individus accepteraient vos opinions sur ce site.

  10. Jack Lynn says:

    THanks.

  11. Jack Lynn says:

    This is great. tree removal Bli Bli

  12. Grant says:

    This is going to be really helpful.
    Visit Here

  13. shemales leeds is very popular web place for sexy chat contacts in UK

  14. site says:

    I was able to find good info from your blog
    posts.
    site

    Which suggests, for these first-timers judi slot is a betger playing on-line gaming choice,
    the best way iit are usuallyy trialled comfortably. But there aree rumors tthe card is not taking place on an island in any respect.
    After wanting at thee prospects of people states within the
    US independently legalizing on-line gambling, the Interactive Investor advises that there
    have been exciting developments, with New Jersey main tthe best
    wway however strikes afoot in Florida, Iowa and California.

  15. Buat article triknya bosku, saya udah daftar pakai
    ovo nih

  16. Shemale Rouen is the most popular web platform for finding casual contacts with hot girls in France

  17. Sex Schleswig-Holstein ist die am schnellsten wachsende Dating-Seite für Männer und Frauen, die einander für einen schnellen und erotischen Kontakt suchen. Die Registrierung ist kostenlos und erfolgt innerhalb kurzer Zeit.

  18. Roksa says:

    Roksa to dobre miejsce, aby umówić się na spotkanie seksualne z nieznajomym i możesz mieć pewność, że profile, które widzisz, są prawdziwe.

  19. Miskolcilanyok egy nagyon hozzáférhető társkereső oldal, ahol a randizásnak nem kell mindig komolynak lennie. Bár ez a társkereső oldal elsősorban a párkapcsolat megtalálására szolgál, más típusú szórakoztató és izgalmas kapcsolatokra is alkalmas.

  20. Sex Frankfurt ist der beste Ort, um einheimische Frauen kennenzulernen. Unsere Frauen stehen auf schnelles Dating, und es dauert nicht lange, bis Sie mit lokalen Frauen intime Gespräche führen und bereit sind, Ihre Durststrecke zu beenden.

  21. Enjoy free chat with hot ladies from France only on our web platform Salope Normandie

  22. Videkilany says:

    Videkilany az egyik legnépszerűbb ingyenes videócsevegő alkalmazás, amely összeköt különböző emberekkel a világ minden tájáról. Úgy tűnhet, hogy ez csak egy újabb csevegőoldal, de a jellemzője, ami egyedivé teszi, az a titoktartás, amit a weboldal fenntart a felhasználók számára, annak ellenére, hogy ingyenes oldal.

  23. Click here says:

    LINQ to SharePoint is a great way to query and update data in SharePoint. It allows you to use the full power of LINQ to query and update data in SharePoint lists. LINQ to SharePoint also provides some additional features that make it easier to work with SharePoint data.

  24. Sm speeltjes says:

    Sm speeltjes is uw one-stop-shop voor bondage spullen. Ze hebben een uitgebreide collectie van al het speelgoed dat je nodig hebt om je diepste fantasieën te vervullen. Sommige van hun top speeltjes zijn dwangmiddelen, bondage meubels, bondage knevels, kuisheid apparaten, bondage kappen, en slaven kragen. Ze hebben meer collecties voor mannelijke kuisheid apparaten dan vrouwelijke (en in andere categorieën, ook), dus dat is iets om op te merken.

Leave a Reply