Tuesday, August 30, 2011

Creating Entities Using the SPMetal Utility & Creating a Visual Web Part that uses LINQ





Open Visual Studio->SharePoint 2010->Visual WebPart->LINQListsPart (as WebPart Name)
-------------------------------------------------------------------------------------------------------
//File Name is "VisualWebPart1UserControl.ascx" -> Open this file & add following code
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="LINQListsPart.VisualWebPart1.VisualWebPart1UserControl" %>

<asp:Literal ID="Display" runat="server" Text="Dispaly"></asp:Literal>

-----------------------------------------------------------------------------------------------
// File name is: "VisualWebPart1UserControl.ascx.cs" -> Open This file & add following code
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using Microsoft.SharePoint.Library;
using System.Text;

namespace LINQListsPart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder writer = new StringBuilder();

            try
            {
                using (EntitiesDataContext dc = new EntitiesDataContext("http://crm-server:30312/"))
                {                   
                    var q = from emp in dc.Employees
                            where emp.Project.DueDate < DateTime.Now.AddYears(5)
                            orderby emp.Project.DueDate
                            select new { emp.Title, Contact = emp.Project.PrimaryContact.Title };


                    writer.Append("<table border=\"1\" cellpadding=\"3\" cellspacing=\"3\">");

                    foreach (var employee in q)
                    {
                        writer.Append("<tr><td>");
                        writer.Append(employee.Title);
                        writer.Append("</td><td>");
                        writer.Append(employee.Contact);
                        writer.Append("</td></tr>");
                    }
                }
            }
            catch (Exception e1)
            {
                writer.Append("<tr><td>");
                writer.Append(e1.Message);
                writer.Append("</tr></td>");
            }
            finally
            {
                writer.Append("</table>");
                Display.Text = writer.ToString();
            }
        }
    }
}

No comments:

Post a Comment

Featured Post

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management https://docs.microsoft.com/en-us/azure/api-management/api-...

Popular posts