Because in ASP. when net1.1, Asp.net does not have the concept of webpart, so the webpart in sps2003 is dependent on Microsoft. sharepoint. DLL, must inherit from the namespace of Microsoft. sharepoint. the base class under webpartpages. To ASP. net2.0 and Asp.net integrate the SharePoint webpart into the Asp.net framework. The new ASP-style webpart depends on system. web. DLL, inherited from a different ASP.. NET 2.0 defines the webpart base class, and its namespace is system. web. UI. webcontrols. webparts. We recommend that you use ASP. net2.0 webpart in Moss.
To use webpart in an ASP. NET 2.0 application, you must create a. ASPX page and include a webpartmanager control and at least one webpartzone control. In Moss, the webpart architecture is based on a control named spwebpartmanager. it overwrites many standard methods of webpartmanager in Asp.net. Similarly, we also need to use Microsoft webpartzone. sharepoint. in the webpartpages namespace, these are all in the default master page.
Moss also provides many out-of-the-box webparts, such as conent editor webpart, data view webpart, list view webpart, image webpart, members webpart, and page viewer webpart. Is the declaration period of the webpart.
Then we will illustrate the development of webpart in Moss through several examples:
I. Upload attachments to the document library
Use the fileupload control in Asp.net to set the website address and the title of the list to be uploaded,CodeAs follows:
Using System; Using ......... Namespace Carywebpart {[GUID (" 288802c4-4dfe-45b6-bb28-49dda89ec225 ")] Public Class Fileuploadwp: system. Web. UI. webcontrols. webparts. webpart {fileupload objfileupload = New Fileupload (); Protected Override Void Createchildcontrols () {controls. Add (objfileupload); button btnupload =New Button (); btnupload. Text =" Save File "; This . Load + = New System. eventhandler (btnupload_click); controls. Add (btnupload );} Private Void Btnupload_click ( Object Sender, eventargs e ){ Using (Spsite objsite = New Spsite (sitecollectionurl )){ Using (Spweb objweb = objsite. openweb (siteurl) {splist objlist = objweb. Lists [listname]; If (Objfileupload. hasfile ){ Try {Objlist. rootfolder. Files. Add (objfileupload. filename, objfileupload. postedfile. inputstream, True );} Catch (Exception ex ){ String A = ex. Message ;}}}}}Private String _ Strsitecollectionurl; [personalizable (personalizationscope. Shared), webbrowsable ( True ),
Webdisplayname (" Website set URL "), Webdescription (" Enter the website set URL ")] Public String Sitecollectionurl { Get { Return _ Strsitecollectionurl ;} Set {_ Strsitecollectionurl =Value ;}} Private String _ Strsiteurl; [personalizable (personalizationscope. Shared), webbrowsable ( True ),
Webdisplayname (" Website URL "), Webdescription (" Enter the website URL ")] Public String Siteurl { Get { Return _ Strsiteurl ;} Set {_ Strsiteurl = Value ;}} Private String _ Strlistname; [personalizable (personalizationscope. Shared), webbrowsable ( True ),
Webdisplayname (" List name "), Webdescription (" Enter a list name. ")] Public String Listname { Get {Return _ Strlistname ;} Set {_ Strlistname = Value ;}}}}
The following is the post-deployment:
Note:
1. personalizable indicates that this attribute is configurable.
2. personalizationscope. Shared sets whether all users of this attribute are visible. You can set both shared and user.
Webbrowsable (true) indicates that it is visible in the editorial component.
3. the URL parameter of an overload method of the openweb method of spsite is the relative address.
4. In the example<Trust Level="Full" Originurl="" />The attachment can be uploaded only when it is set to full.
II:WebpartUse resources in
There are two ways to access resources: Put the resources under _ layouts and web resources, which can be compiledProgramSet, we mainly talk about web resources. Create a new project, add an image to the project, and add the following code to assemblyinfo. CS:
[Assembly: webresourceattribute ("Webresource.icons.recycbin.gif","Image/GIF")]
Image/GIF indicates the resource type. If it is JS, it is written as text/JavaScript.
Then, you need to create a virtual class. This class does not take any action and is only used to obtain the resource reference. The following code is the class:
UsingSystem;Using.........NamespaceWebresource {
Public ClassPlaceholder {}}
Use the following code to obtain web resources:
StringStrresource ="Webresource.icons.recycbin.gif";
StringStrurl = page. clientscript. getwebresourceurl (Typeof(Webresource. placeholder), strresource );
The obtained URL is in the following format: "/webresource. axd? D = [Assembly key] & t = [last write time of resource assembly]"
/Webresource. axd? D = A34Cgk8tSLsOaGW2Mtx1-iMMZgj-CEJ77updNPilqoda3ab0CWc1mJqVyolRohMp0 & t = 633457672629813944
The following code uses the bulletedlist control and sets the icon of the Control item as an image in our Web Resource:
Namespace Carywebpart { Public Class Webresourcewp: system. Web. UI. webcontrols. webparts. webpart { Protected Override Void Createchildcontrols (){ String Strresource =" Webresource.icons.recycbin.gif "; String Strurl = page. clientscript. getwebresourceurl ( Typeof (Webresource. placeholder), strresource); bulletedlist objbullist = New Bulletedlist (); objbullist. bulletstyle = bulletstyle. customimage; objbullist. bulletimageurl = strurl; objbullist. Items. Add (" One "); Objbullist. Items. Add ("Two "); Objbullist. Items. Add (" Three "); Objbullist. Items. Add (" Four "); Controls. Add (objbullist );}}}
Yes:
III:Webpart Action menu
There are three types:
1. client-side verbs: executed on the client. When using the webpartverb class, the constructor needs to pass two parameters. The first is the verbs ID, and the second is the processing program for clicking, is a JS program. The Code is as follows:
Webpartverb objfirst =NewWebpartverb ("Firverbid","Javascript: Alert ('Hello verb! ');"); Objfirst. Text ="First verb"; Objfirst. Description ="Description of the first verb"; Objfirst. imageurl ="_ Layouts/images/test/recycbin.gif";
2. server-side verbs: run on the server side. The event handler in the parameter uses the server-side code.
Webpartverb objsecond =NewWebpartverb ("Secverbid",NewWebparteventhandler (secondverbhandler); objsecond. Text ="Second verb"; Objsecond. Description ="Description of the second verb";
3. BOTH: this action is executed on both the server side and the client side. You can use webpartverbcollection.
Webpartverb [] objverbs =NewWebpartverb [] {objfirst, objsecond}; webpartverbcollection objverbcollection =NewWebpartverbcollection (Base. Verbs, objverbs );
The complete code is as follows:
UsingSystem;Using.........
Namespace Carywebpart {[GUID (" 288802c4-4dfe-45b6-bb28-49dda89ec225 ")] Public Class Verbswp: system. Web. UI. webcontrols. webparts. webpart {Public Override Webpartverbcollection verbs { Get {Webpartverb objfirst = New Webpartverb (" Firverbid "," Javascript: Alert ('Hello verb! '); "); Objfirst. Text =" First verb "; Objfirst. Description =" Description of the first verb "; Objfirst. imageurl =" _ Layouts/images/test/recycbin.gif "; Webpartverb objsecond =New Webpartverb (" Secverbid ", New Webparteventhandler (secondverbhandler); objsecond. Text =" Second verb "; Objsecond. Description =" Description of the second verb "; Webpartverb [] objverbs = New Webpartverb [] {objfirst, objsecond}; webpartverbcollection objverbcollection = New Webpartverbcollection ( Base . Verbs, objverbs ); Return Objverbcollection ;}} Protected Void Secondverbhandler ( Object Sender, webparteventargs ARGs ){}}}
For example:
IV:Create an editorial component
All editorial components are integrated from the editorpart class. The code of the webpart to be edited is as follows:
UsingSystem;
Using.........
Namespace Carywebpart {[GUID (" 288802c4-4dfe-45b6-bb28-49dda89ec225 ")] Public Class Foreditwp: system. Web. UI. webcontrols. webparts. webpart {Protected Override Void Rendercontents (htmltextwriter writer) {writer. Write (" Testvalue: "+ Testvalue );} Public Override Editorpartcollection createeditorparts () {editwp objeditor = New Editwp (); objeditor. ID = ID +" Testeditor1 "; Objeditor. Title =" Test editor title "; Objeditor. tooltip =" Test editor tooltip "; Objeditor. tabindex = 100; objeditor. groupingtext =" Test editor grouping text "; Arraylist objeditorparts = New Arraylist (); objeditorparts. Add (objeditor); editorpartcollection objeditorpartscollection = New Editorpartcollection (objeditorparts ); Return Objeditorpartscollection ;} Private String Strnormalvalue = string. Empty; [personalizable (personalizationscope. Shared), webbrowsable ( False ), Webdisplayname ("Normal Value "), Webdescription (" Normal value description ")] Public String Testvalue { Get { Return Strnormalvalue ;} Set {Strnormalvalue = Value ;}}}}
Createeditorparts: Create an instance of the custom editorpart control associated with the webpart control. This method is called when you click Edit action on the webpart control. Then develop the editorial component corresponding to the preceding webpart. The Code is as follows:
UsingSystem;Using.........
Namespace Carywebpart { Public Class Editwp: editorpart {textbox txtnormalbox; Protected Override Void Createchildcontrols () {txtnormalbox = New Textbox (); txtnormalbox. ID =" Txtnormalbox "; Txtnormalbox. Text =" [Custom editor part] "; Txtnormalbox. textmode = textboxmode. multiline; txtnormalbox. rows = 5; controls. Add (txtnormalbox );}Public Override Bool Applychanges () {foreditwp objnormal = (foreditwp) webparttoedit; objnormal. testvalue = txtnormalbox. text; Return True ;} Public Override Void Syncchanges () {ensurechildcontrols (); foreditwp objnormal = (foreditwp) webparttoedit; txtnormalbox. Text = objnormal. testvalue ;}}}
Applychanges method: Save the value entered by the user in the editorpart control to the corresponding property of the webpart control referenced in the webparttoedit attribute.
The syncchanges method corresponds to the preceding method. It is responsible for synchronizing the values in the editorpart control with those in the associated webpart control.
For example:
V. Deployment
The general process of deploying a webpart in Moss:
1. Build a development environment, create a webpart project, and write code.
2. Modify the Assembly. CS File
Before deployment, modify the Assembly file and add the following two sentences:
Using System. Security;
[Assembly: allowpartiallytrustedcallers]
If the preceding modification is not performed, a message indicating a failure will be prompted during webpart installation.
3. Copy an object
Copy the compiled DLL to the bin directory under the web application directory. The folder location of the Web application is similar to the following path:
C: \ Inetpub \ wwwroot \ WSS \ virtualdirectories \ 80 \ bin.
4. modify web. config
Before using webpart, you must modify the configuration file of the Web application.
(1) Add a line in safecontrols, similar to the following statement.
< Safecontrol Assembly = "Hellowebpart" Namespace = "Hellowebpart" Typename = "*" Safe = "True" Allowremotedesigner = "True" />
(2) modify the trust level.
Set< Trust Level = "Wss_minimal" Originurl = "" /> Change to <trust level =" Wss_medium "Originurl =" "/> You can also change wss_medium to full.
5. Add a webpart to the website (website operations-website settings-Web parts-New)
Vi. debugging
Again, the w3wp.exe process can be added, and then the breakpoint can be set.