ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Showing posts with label Basic WebPart. Show all posts
Showing posts with label Basic WebPart. Show all posts

Monday, October 14, 2013

Custom properties for visual webpart 2010

Custom properties for visual webpart
You should read 3 article first
http://mstechsharing.blogspot.com/2012/03/lesson-04-webpart-property-and-default.html
http://mstechsharing.blogspot.com/2012/03/lesson-05-more-attribute-and-custom.html
http://mstechsharing.blogspot.com/2012/03/lesson-06-validation-in-webpart-and.html

Create new Visual webpart project


Add new class for toolpart class

Open CustomPropertiesForVisualWebpart.cs and Replace this code
using System.ComponentModel;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;

namespace CustomPropertiesForVisualWebpart.CustomPropertiesForVisualWebpart
{
    [ToolboxItemAttribute(false)]
    public class CustomPropertiesForVisualWebpart : WebPart
    {
     
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/CustomPropertiesForVisualWebpart/CustomPropertiesForVisualWebpart/CustomPropertiesForVisualWebpartUserControl.ascx";

        protected override void CreateChildControls()
        {
            CustomPropertiesForVisualWebpartUserControl control = (CustomPropertiesForVisualWebpartUserControl)Page.LoadControl(_ascxPath);
            //Assign value to Attribute of webpart user control
            control.Attributes.Add("DisplayCar", DisplayCar);
            Controls.Add(control);
        }

        #region properties
        public string _value;// public static string _value; (Note: remove static)
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        string displayCar = "1";
        public string DisplayCar
        {
            get { return displayCar; }
            set { displayCar = value; }
        }
        #endregion
        /// <summary>
        /// return all toolpat
        /// </summary>
        /// <returns>return toolpart</returns>
        public override ToolPart[] GetToolParts()
        {
            ToolPart[] allToolParts = new ToolPart[3];
            WebPartToolPart standardToolParts = new WebPartToolPart();
            CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();
            allToolParts[0] = standardToolParts;
            allToolParts[1] = customToolParts;
            allToolParts[2] = new CustomToolPart();
            return allToolParts;
        }
    }
}
Open CustomToolPart and replcae this code
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Data;


namespace CustomPropertiesForVisualWebpart.CustomPropertiesForVisualWebpart
{
    public class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        #region variable
        /// <summary>
        /// declare variable
        /// </summary>
        Label lblError;
        DropDownList ddlDisplayCar;
        TextBox txtValue;
        #endregion
        /// <summary>
        /// Create Control
        /// </summary>
        protected override void CreateChildControls()
        {
            try
            {
                lblError = new Label();
                Label lblDisplayCar = new Label();
                lblDisplayCar.Text = "Choose display car: <br />";
                ddlDisplayCar = new DropDownList();
                ddlDisplayCar.ID = "ddlDisplayCar";
                ddlDisplayCar.Items.Add(new ListItem("All Cars", "1"));
                ddlDisplayCar.Items.Add(new ListItem("All Cars of Current User", "2"));
                txtValue = new TextBox();

                this.Controls.Add(lblError);
                this.Controls.Add(lblDisplayCar);
                this.Controls.Add(ddlDisplayCar);
                this.Controls.Add(txtValue);
                DisplayDataFromWebpartToToolPart();
            }
            catch (System.Exception ex)
            {
                lblError.Text = ex.Message;
                //You can write log here
            }
        }
        /// <summary>
        /// Display Data From Webpart To ToolPart
        /// </summary>
        private void DisplayDataFromWebpartToToolPart()
        {
            //Display data from webpart to toolpart
            CustomPropertiesForVisualWebpart customPropertiesVisualWP = (CustomPropertiesForVisualWebpart)this.ParentToolPane.SelectedWebPart;
            // DisplayCar
            ListItem currentDisplayCar = ddlDisplayCar.Items.FindByValue(customPropertiesVisualWP.DisplayCar);
            if (null != currentDisplayCar)
            {
                ddlDisplayCar.SelectedValue = currentDisplayCar.Value;
            }

            txtValue.Text = customPropertiesVisualWP.Value;
        }
        /// <summary>
        /// Apply changes when we click button Apply and Ok in toolpart panel
        /// </summary>
        public override void ApplyChanges()
        {
            CustomPropertiesForVisualWebpart customPropertiesVisualWP = (CustomPropertiesForVisualWebpart)this.ParentToolPane.SelectedWebPart;
            customPropertiesVisualWP.DisplayCar = ddlDisplayCar.SelectedValue;
            customPropertiesVisualWP.Value = txtValue.Text;
        }
    }
}
Open CustomPropertiesForVisualWebpartUserControl.ascx and Add 2 Label then open CustomPropertiesForVisualWebpartUserControl.ascx.cs and replcae this code
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace CustomPropertiesForVisualWebpart.CustomPropertiesForVisualWebpart
{
    public partial class CustomPropertiesForVisualWebpartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblCarWay1.Text = Convert.ToString(this.Attributes["DisplayCar"]);
            lblCarWay2.Text = CustomPropertiesForVisualWebpart._value;
        }
    }
}
Build and Deploy then add webpart => edit properties webpart


Friday, March 2, 2012

Lesson 01: Basic of sharepoint webPart


-        Hiểu cơ bản về Webpart trong C# 2005, 2008
-        Bạn phải có một Web Application và 1 Top-level Site, tương ứng với cổng bạn tạo (ví dụ: mặc định là cổng 80).
-        Mở VS 2008
-        Tạo mới một Solution  WebPartSoln trênC:\TênFolder chứa Solution  (WebpartSoln)
-        Chép Microsoft.Sharepoint.Dll vào Solution.

Bài 1: Tạo Strong Name trong ứng dụng C#
-        Nơi tạo: có thể tạo trực tiếp trong Solution hoặc là tạo ở đâu rối chép vào Solution.
-        Mở Microsft Visual Studio 2008 -> Visual Studio 2008 -> Visual Studio 2008 Command Prompt
-        C:\Program Files\Microsoft Visual Studio 9.0\VC>cd /
-        C: \>sn –k  hkgsolution.snk
-        sau khi câu lệnh thực thi bạn có thể vào C:\>  để có thể nhanh thấy Strong Name (.snk) bạn vừa tạo.

-        Cắt hkgsolution.snk đến folder WebpartSoln

Bài 2: Hiểu dùng StrongName(hkgsolution.snk) trong C#
-        Thêm mới một Webpart Project vào WebpartSoln
-        Chọn Class Clibrary Project và đặt tên là HelloWebPart
Chọn Class1 từ Solution Explorer
Đổi tên thành HelloWebPart
-        Vào thư mục Properties trong Webpart Project mở file AssemblyInfo.cs
Di chuyển xuống phần cuối cùng của File AssemblyInfo.cs, thêm vào : (tùy với vị trí đặt Hkgsolution.snk).
[assemblyAssemblyKeyFile("hkgsolution.snk")]
[assemblyAssemblyKeyFile("..\\hkgsolution.snk")]
[assemblyAssemblyKeyFile("..\\.\\hkgsolution.snk")]

-        Build project ở chế độ Release.
-        Chắc chắn rằng Build thành công.


Bài 3: Mở Webpart Project
-        Tiếp tục thực hiện với HelloWebpart Project
-        Click vào References | R-Right | Add Reference
-        Chọn Microsoft.Sharepoint.Dll vào  thư mục của  WebPartSoln
-        Tiếp tục chọn Tab .NET | System.Web
Chắc chắn rằng bạn có khai báo đầy đủ các Namespace
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
Và HelloWebPart Class này phải kế thừa từ Webpart
    public class HelloWebPart:WebPart
{
//do things;
}
-        Build project ở chế độ Release.

Bài 4: Tếp tực thực hiện với Class HelloWebPart
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: Render
-        Và đánh vào như đoạn code bên dưới:
public class HelloWebPart:WebPart
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("Hello My HKG Web Part!");
            base.Render(writer);
        }
    }
-        Build project ở chế độ Release.

Bài 5: Hiểu cách để tạo ra Public Key Token như thế nào?
-        Mở Microsft Visual Studio 2008 -> Visual Studio 2008 -> Visual Studio 2008 Command Prompt
-        C:\>cd C:\ WebPartSoln\ HelloWebPart\bin\Release ; nhấn Endter
-        C:\ WebPartSoln\ HelloWebPart\bin\Release>sn –T  HelloWebPart.dll
[=>Đây là đường dẫn của Project chứa HelloWebPart.dll khi bạn build theo chế độ Release.
Tên của .dll trùng với tên namespace của Project]
-        Sau khi thành công thì nó sẽ tạo ra 1 public key Token, nhớ chuỗi ký tự số này.
-        C:\>cd C:\ WebPartSoln\ HelloWebPart\bin\Release>exit
Bài 6:
-        Tếp tực thực hiện với Class HelloWebPart
-        Chọn vào Project | Add New Item
-        Chọn Text File và đặt tên là HelloWebPart.dwp -> Nhấn Add
-        Mô tả nội dung của File này như sau:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
            <Title>Hello WebPart</Title>
            <Description>Hello WebPart</Description>
(1)<Assembly>HelloWorldPart, Version=2.0.0.0, Culture=neutral, (2)PublicKeyToken= ec47e7d9465cc36e </Assembly>
            (3)<TypeName> HelloWorldPart. HelloWorldPart</TypeName>
            <!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>
(1) Tên của file HelloWorldPart.dll, Version=2.0.0.0: Version trong File AssemblyInfo.cs
(2)  ec47e7d9465cc36e Public Key Token đã tạo ra ở bài 4
(3) Tên của  NameSpace.Tên của Class WebPart
-        Build Project ở chế độ Release

Bài 7: Hiểu để triển khai 1 Webpart đơn giản như thế nào?
-        Truy cập vào Sharepint Server by Remote DeskTop Connection
+       Vào: C:\inetpub\wwwroot\wss\VirtualDirectories\
+       R- Click vào Your Port (ví dụ: port 80) và Share thư mục với tên là Mysite
-        Trở về máy PC
+       Chọn Mysite folder | R –Click| Map Network Drive và chon Y  (nếu sử dụng máy ảo ><sử dụng win server đương dẫn sẽ là inetpub\wwwroot\wss\VirtualDirectories\ như hình bên dưới)
+       Copy HelloWorldPart.dll vào Y:\Bin folder (thư mục Mysite)
+       Copy Microsoft.Sharepoint.Dll vào Y:\Bin folder (thư mục Mysite)
+       Copy HelloWebPart.dwp vào Y:\Bin\wpcatalog folder (thư mục Mysite)
+       Trở về Top-level hoặc Sub Site
+       Click  Site Actions  | Edit page hyper link
+       Click  Add Web Part  và chọn Server libraries
+       Chọn vào HelloWorldPart và thấy xuất hiện lỗi?

Bài 8: Mô tả SafeControl trong Web.config
-        Chọn vào Y:\ và chọn Web.config
-        R-Click | chỉnh bằng NotePad
-        Di chuyển đến vị trí </ SafeControl>
-        Và mô tả < SafeControl> như trong Slide của bạn
-        Trở về Top-level hoặc Sub Site
-        Click  Site Actions  | Edit page hyper link
-        Click  Add Web Part  và chọn Server Gallery
-        Chọn HelloWorldPart và thấy lỗi?

Bài 9: tạo ra 1 sub Site mới từ Top Level Site của bạn:
-        Click  Site Actions  | Edit page hyper link
-        Click  Add Web Part  và chọn Server Gallery
-        Chọn HelloWorldPart và thấy lỗi?
-       

Bài 10: Hiểu thay đổi số Version trong C# như thế nào?
-        Mở  Folder Properties  và chỉnh sửa AssemblyInfo.cs
-        Thay đổi AssemblyInfo Version từ 1.0.0.0 thành 2.0.0.0
-        Build lại Project ở chế độ Release
-        Copy lại HelloWorldPart.dll vào Y:\Bin
-        Trở về Top-level hoặc Sub Site
-        Refesh lại page và thấy xuất hiện lỗi gì?
-        Giải quyết vấn đề như tếh nào?

Bài 11:
-        Add new Web Part vào WebPartSoln
-        Chọn Class Clibrary Project và đặt tên là OverrideMethodWebPart
-        Chọn Class Clibrary Project và đặt tên là HelloWebPart
-        Chọn Class1 từ Solution Exployere
-        Đổi tên thành OverrideMethodWebPart
-        Click vào References | R-Right | Add Reference
-        Chọn Microsoft.Sharepoint.Dll vào  thư mục của  WebPartSoln
-        Tiếp tục chọn Tab .NET | System.Web
-        Chắc chắn rằng bạn có khai báo đầy đủ các Namespace
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
-        Và OverrideMethodWebPart Class này phải kế thừa từ Webpart
  
 public class OverrideMethodWebPart:WebPart
{
}
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: Render
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("Render!"+current time);
            base.Render(writer);
        }
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: OnPreRender
protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }   
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: CreateChildControls
protected override void CreateChildControls()
        {

            TextBox box = new TextBox();
      box.Text = "CreationChildControls" +        DateTime.Now.ToLongTimeString();
            this.Controls.Add(box);
            base.CreateChildControls();
        }
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: OnLoad

protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: OnInit

protected override void OnInit(EventArgs e)
        {
          
            base.OnInit(e);
       {
}
               
-        Đánh protected override nhấn spacebar
-        Chọn phương thức: EnsureChildControls

protected override void EnsureChildControls()
        {
            base.EnsureChildControls();
        }

-        OverrideMethodWebPart.dwp
+       Project | Add New Item | Select Text File
+       Đặt tên: OverrideMethodWebPart.dwp
+       Copy nội dung từ  HelloWorldPart.dwp và thay đổi 1 vài thứ

-        Mở Properties Folder và chỉnh sửa file  AssemblyInfo.cs
Di chuyển xuống phần cuối cùng của File AssemblyInfo.cs, thêm vào : (tùy với vị trí đặt Hkgsolution.snk).
[assemblyAssemblyKeyFile("hkgsolution.snk")]
[assemblyAssemblyKeyFile("..\\hkgsolution.snk")]
[assemblyAssemblyKeyFile("..\\.\\hkgsolution.snk")]
-        Build project ở chế độ Release.
-        Triển khai Web part vào Web Site của bạn:
-        Copy OverrideMethodWebPart.dll vào thư mục Y:\Bin
-        Chỉnh sửa Web.config và mô tả lạ SafeControl Tag