[Feature] use feature to import webpart

Source: Internet
Author: User

When I first came into contact with webpart a few days ago, I found an article titled importing webpart with feature.ArticleAt that time, I had no knowledge about feature, so I read it for a while. When I looked back today, I found that the content in the article was "two missing pounds" (I was dizzy ), depressed, it's hard to find it in English on Google, I finally found another article "add a Web part to your web parts gallery using a feature" (use feature to add a webpart to your webpart library ?).

The content is very simple, and it is easy to understand because of the reference in the previous article. However, after a try, I found that there were a lot of problems. It seems that the authors only wrote specific steps, ignoring the small details (--) that should be paid attention to, er, as a result, we can only try it out with a hard head (here we are very grateful to Mr. Google for his help). Finally, the moment of a brilliant shot has finally arrived!

 

[1] to put it simply, we need a completed webpart, for example, webpartforleadingin. dll.

Using System;
Using System. text;
Using System. Web. UI. webcontrols. webparts;

NamespaceWebpartforleadingin
{
Public ClassWP: webpart
{
Private String_ Text= "See, I imported the webpart ~ through feature ~";

[Webbrowsable ( True ), Personalizable ( True )]
Public   String Text
{
Get { Return _ Text ;}
Set {_ Text = Value ;}
}
Protected   Override   Void Render (system. Web. UI. htmltextwriter writer)
{
Writer. Write (_ text );
}
}
}

Do not need to mention the strong name, and then compile...

 

[2] At the same time, it is best to manually compile a. webpart file. This file is generally not generated when it is encoded in Vs and is available only after it is imported to SharePoint. It is an XML file. (I name it WP. webpart)

<? XML version = "1.0" encoding = "UTF-8" ?>
< Webparts >
< Webpart Xmlns = "Http://schemas.microsoft.com/WebPart/v3" >
< Metadata >
< Type Name = "Webpartforleadingin. WP, webpartforleadingin, version = 1.0.0.0, culture = neutral, publickeytoken = 437f6f449ff02b7a"   />
< Importerrormessage > This Web part cannot be imported. </ Importerrormessage >
</ Metadata >
< Data >
< Properties >
< Property Name = "Title" Type = "String" > WP </ Property >
</ Properties >
</ Data >
</ Webpart >
</ Webparts >

 

[3] The webpart is completed in the preceding two steps. The feature is written below. XML and element. XML. enter the same file name for the related part in the XML file)

◎ Feature. xml file:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Feature Xmlns = "Http://schemas.microsoft.com/sharepoint"
ID = "FC3BB8FB-EC18-4ace-9B1D-24333C2C9A6E"
Title = "Leading in webpart"
Description = "Importing webpart through feature"
Hidden = "False"
Scope = "Site" >
< Elementmanifests >
< Elementmanifest Location = "Element. xml"   />
</ Elementmanifests >
</ Feature >

◎ Element. xml file:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Elements Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Module
Name = "Webpartpopulation"
List = "113"
URL = "_ Catalogs/WP"
Rootwebonly = "True" >
< File URL = "WP. webpart" Type = "Ghostableinlibrary"   />
</ Module >
</ Elements >

 

[4] The next step is great. I wrote. bat batch files, saving me the effort of typing commands in cmd, and "one step in place ":)

◎ Importwp. BAT file:

@ Set stsadm = "C: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ bin \ stsadm"

Echo installing feature

% Stsadm %-O installfeature-filename webpartforleadingin \ feature. XML-Force

Echo activating feature

% Stsadm %-O activatefeature-filename webpartforleadingin \ feature. XML-URL http: // gl-5fc632fd62e9: 8800/

Iisrest

 

To modify your profile, such as the location of stsadm.exe, webpartforleadingin is a folder name, http: // gl-5fc632fd62e9: 8800/is a Sharepoint site URL.

[5] If you do this, everything will be ready.

Set webpartforleadingin. copy the DLL to GAC (c: \ windows \ assembly) and copy the feature. XML, element. XML, importwp. bat, WP. copy the four webprt files to the webpartforleadingin folder and move them to C: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ Features.

(* Note the following two points:

1) modify web. config under the corresponding website and add the safecontrol node,Mine is<Safecontrol Assembly = "webpartforleadingin, version = 1.0.0.0, culture = neutral, publickeytoken = 437f6f449ff02b7a" namespace = "webpartforleadingin" typename = "*"/>.I feel a little dizzy. Isn't the webpart file put under GAC completely credible? How can I set a security node?

2) An error may occur when feature is activated later,

I did this by myself: [SharePoint 3.0 Management Center]-> [ApplicationProgramManagement> application security> Web application Policy> select the website to be activated, [Add User]-> [region] Select "all regions" (you can also select the corresponding region, such as the default) -> [next]-> Fill in the website administrator account in the "user" box, and set "permission" to "Full Control"-> [complete]

)

[6] Run importwp. BAT and go to your website to see if there is anything more. My website is like this:

 

Well, if you are interested, go to the "Web parts" library to see if something is new (WP. webpart! New). Jesus, what are you waiting for? Add it to the page directly ~

 

good luck :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.