Un esempio di come creare una Web Part in SharePoint 2007 (MOSS) in pochi passi:
  • crea, in Visual Studio 2005, un progetto di tipo Class Library con nome Sgart.WebPart
  • imposta i riferimenti a System.Web e System.Data
  • crea il file Hello.cs (riportato di seguito)
  • vai nella directory dove c'è il sito SharePoint (es: c:\inetpub\wwwroot...) ed inserisci nel web.config sezione SafeControls la seguente linea:

    XML

    <SafeControl Assembly="Sgart.WepPart" Namespace="Sgart.WP" TypeName="*" />
    
  • compila e copia il file Sgart.WebPart.dll nella directory bin del sito
  • fai un iisreset (oppure ricicla l'application pool)
  • vai in Site Actions / Site Settings / Modify All Site Settings / (Gallery) Web Parts e premi new, seleziona il file Hello e premi populate (questa operazione crea automaticamente il file dwp che in WSS2 andava fatto a mano)
  • vai in una pagina del sito e premi Site Action / Edit Page, poi premi Add a Web Part cerca la web part Hello ed aggiungila alla pagina
Hai appena creato la tua prima Web Part per SharePoint.

C#: Hello.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls ;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace Sgart.WP
{
    /// <summary>
    /// Hello.cs
    /// simple web part
    /// </summary>
    /// <example>
    /// output path c:\inetpub\wwwroot\bin
    /// add to web.config section SafeControls
    /// <SafeControl Assembly="Sgart.WepPart" Namespace="Sgart.WP" TypeName="*" />
    /// go to site actions / site settings / web parts gallery
    /// click new and select webpart then press populate to make dwp file
    /// </example>
    [XmlRoot(Namespace="SgartWebParts")]
    public class Hello : System.Web.UI.WebControls.WebParts.WebPart
    {
        public Hello()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        //private string _listName = "";
        //[WebBrowsable(true), 
        //  Personalizable(PersonalizationScope.Shared),
        //  WebDisplayName("Nome lista"),
        //  WebDescription("Descrizine della webpart"),
        //  Category("Test")]
        //public string ListName
        //{
        //    get { return _listName; }
        //    set { _listName = value; }
        //}

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            //base.RenderContents(writer);
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
            if (ci.Name.StartsWith("it") == true)
            {
                writer.Write(
                    string.Format("Ciao {0} la data attuale è {1}"
                        , this.Context.User.Identity.Name
                        , DateTime.Now.ToString(ci)));
            }
            else
            {
                writer.Write(
                    string.Format("Hello {0} the date is {1}"
                        , this.Context.User.Identity.Name
                        , DateTime.Now.ToString(ci)));
            }
        }
    }
}
Le Wep Part in WSS3 vengono create partendo dalla classe WebPart del framework 2.0 e non più dal name space Microsoft.SharePoint.
Questa web part dimostra anche come visualizzare le date nel modo corretto (System.Globalization.CultureInfo.CurrentCulture), basandosi sulle impostazioni internazionali impostate nel sito (vedi Site Actions / Site Settings / Modify All Site Settings / Regional Settings)
Tags:
C#235 .NET66 SharePoint498 SharePoint 2007218 WSS4
Potrebbe interessarti anche: