Showing posts with label .NET Framework. Show all posts
Showing posts with label .NET Framework. Show all posts

Sunday, October 2, 2011

How to make LINQ query to Sharepoint 2010 across Site Collections

 I'm currently member in a development team, working on a project on Sharepoint 2010, and one of the tasks is to execute LINQ query on Sharepoint objects, that's ok, seems quite simple as we have experience on LINQ.

But when we tried to apply the same query across site collections we realised that despite the fact that the correct site collection is opened, we couldn't read the objects.

During searching on the internet we found a workarround by changing the SPContext before executing the LINQ query.

In the webpart itself the HttpContext is stored in a variable before executing our search method.


We have found the cause of this problem. When an SPContext is available (like in the webpart), the constructor of SPServerDataConnection tries the open the url in the SPSite of the context.

So querying a linq datasource in a different site collection doesn't work whenever an SPContext object is available.

We managed to reproduce this problem in our console application by createing a fake SPContext object which causes the same error.

Microsoft.Sharepoint.Linq.Provider - SPServerDataConnection:

public SPServerDataConnection(string url)
{
    if (SPContext.Current != null)
    {
        this.defaultSite = SPContext.Current.Site;
        this.defaultWeb = (SPContext.Current.Web.Url == url) ? SPContext.Current.Web : this.defaultSite.OpenWeb(new Uri(url).PathAndQuery);
    }
    else
    {
        this.defaultSite = new SPSite(url);
        this.defaultWeb = this.defaultSite.OpenWeb(new Uri(url).PathAndQuery);
    }
    if (!this.defaultWeb.Exists)
    {
        throw new ArgumentException(Resources.GetString("CannotFindWeb", new object[] { url }));
    }
    this.defaultWebUrl = this.defaultWeb.ServerRelativeUrl;
    this.openedWebs = new Dictionary();
    this.openedWebs.Add(this.defaultWebUrl, this.defaultWeb);
}

So, we tried to cheat on the application, below you can see the solution:

In the webpart itself the HttpContext is stored in a variable before executing our method.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
     using (SPSite contextSite = new SPSite("http://"))
     {
          using (SPWeb contextWeb = contextSite.OpenWeb())
          {
               HttpRequest httpRequest = new HttpRequest("", contextWeb.Url, "");
               HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
               SPControl.SetContextWeb(HttpContext.Current, contextWeb);               
               using (MyLinqDataContext dc = new MyLinqDataContext("http://"))
               {
                    EntityList entities = dc.GetList("");
                    {
                    }
               }
           }
     }
}


After performing the query we set the HttpContext back to the original HttpContext. 


tempContext = HttpContext.Current;
this.Search();
HttpContext.Current = tempContext;


I'm not sure if this workaround is appropriate, but at least we can now perform our cross site collection LINQ query in our webpart.

Wednesday, June 22, 2011

Some ways to use Sharepoint

I’ve found that people have a general idea of what SharePoint is, but many have no idea how to apply it to their own business and as a result they spend a lot of valuable time to analyse, design and develop applications for their needs. So, I’d like to share some ways that SharePoint can solve your common business needs.

I want to emphasize that listed ways are only a few of the many ways to utilize the SharePoint Services. Once you start using it, you will begin to realize the many other valuable benefits that can easily be accomplished by the everyday user. Let’s see if you can find a few productivity solutions here that you've been trying to find for your own business, organization or daily routine.


With SharePoint you can:
  • SharePoint helps you build powerful business solutions using new features such as Language Integrated Query (LINQ) to SharePoint, OData Services, and the Client Object Model. You also can use Business Connectivity Services to pull external data, complete with read/write capability.
  • Collaborate with team members on all documents and stay on top of who did what. Earlier versions can easily be restored in case someone has made too many mistakes. Projects can also be linked to related documents.
  • Keep a central task location for assigning tasks to team members. These tasks will automatically show up in your team's Outlook 2007 To-Do List. Those tasks will also link to your projects so you can easily find out what tasks are still open for each project.
  • Store all your emails on a secure and centralized Website for easy archive.
  • Helps in project management by assigning tasks to your team members, and automatically notify them that they have a new task. Alerts are sent when there have been updates to the tasks.
  • Organize large events and store the related documents, assigned tasks, and generally post anything and everything related to the events. It will also integrate with Outlook for added efficiency.
  • Manage all projects for your team or organization so there's no need to explore buying an expensive project management solution.
  • Implement a help ticket resolution for your organization or  team without breaking the bank.
  • Use the efficient check-in / check-out management feature to sort your documents.
  • Start a private company blog to communicate and share ideas with your team that's viewable only by those you give access to.
  • Gain more control over your company's documents with the content approval function.
  • Offer training materials to your teams, clients, and/or partners in a password-protected Website that can be accessed anywhere in the world.
  • Offer a secure and private place to share documents and other information with clients and/or partners.
  •  Create better team communication and brainstorming sessions where everyone can participate when their schedules permit.
  • Centralize where company and team project announcements are posted. Everyone will receive a notification via email or mobile phone automatically, anywhere in the world.
  •  Access and work with your data using your Internet-enabled mobile phone for added convenience while traveling or out of the office at client meetings.
  •  Create "central" documents (and syncronize), so all team members, clients, and/or partners are able to work on the same document and make changes. Updates are accessible with a click of a button. Everyone can then sync back to the "central" document and have all edits merged into that single document.
  • Create a project dashboard where on one page you can view and filter on common project elements, such as: project details, project documents, project tasks, project issues, project calendar, project milestones, project lessons learned, project risks, project change orders, and more.
  • Pull up and update Microsoft Access 2007 database from a local desktop and sync information to a central location that can be accessed from any where at any time.
  •  Easily add custom fields to any area and capture the information that's most important to your company, all without the help of a web designer or IT person.
  •  Work offline on the files, project tasks, tasks, discussions, contacts, calendars, blogs, etc. and then sync the updated information later on.
  • Create "central" documents (and syncronize), so all team members, clients, and/or partners are able to work on the same document and make changes. Updates are accessible with a click of a button. Everyone can then sync back to the "central" document and have all edits merged into that single document

And last but not least, saves you from spending a fortune to meet these communication, collaboration, and organizational needs.

Wednesday, November 25, 2009

Choosing Between MVC and MVP Patterns in ASP.NET

Lately, ASP.NET developers have been paying increasing attention to the architectural aspect of their applications. However, little effort usually is done to choose the right architectural solution. Developers often make a blind choice in favour of the Model-View-Controller pattern and, specifically, ASP.NET MVC framework, disregarding other possible solutions.

Nevertheless, alternative architectural solutions, such as Model-View-Presenter and corresponding frameworks (MVC#, for instance) do prove themselves more useful than MVC in many situations. That is why ASP.NET developers and architects should be aware of all advantages of MVP pattern over MVC and vice versa to choose the appropriate pattern to be used in a specific application. This article provides guidelines for ASP.NET developers on choosing the right pattern among MVC and MVP, concerning all major differences between them.

Choice criteria

1. UI library used
2. Controller-View linking
3. Portability to other UI platforms
4. Tasks support



You can whole article below:
http://www.codeguru.com/csharp/.net/net_general/patterns/article.php/c15173/

Article Author: Oleg Zhukov