Obtain and modify any webpart in the SharePoint site

Source: Internet
Author: User

After moving to the new home, take a rest and continue to study Sharepoint.

When it comes to webpart, anyone who has access to SharePoint will first come into contact with this, which is not explained here. Because a demo of A SharePoint site was recently launched, it was found that the webpart provided by Sharepoint is often used. Any document library, image library, and so on generated when you set the site will generate a webpart. How can you modify it? We can create our own webparts as needed. When a page has many webparts, especially the webparts generated by the system, it is convenient and practical, but sometimes it is not exactly what we want. How can we get and modify it?

See the following section.Code:

 

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;

Namespace Liang. listwebpart
{
Public   Class List: webpart
{
Spweb spw =   Null ;
Protected   Override   Void Onload (eventargs E)
{
Base . Onload (E );
Mytest ();
}
Public   Void Mytest ()
{
Webpartmanager wpm = Webpartmanager. getcurrentwebpartmanager (PAGE );
Webpartcollection WPC = Wpm. webparts;
Spsite SPS =   New Spsite ( @" Http: // mossserver: 9002/sites/bussiness/doclib5/Forms/allitems. aspx " );
// Spweb spw = spcontrol. getcontextweb (context );
Spweb spw = SPs. allwebs [ 0 ];
Spw. allowunsafeupdates =   True ;

Foreach (Webpart WP In WPC)
{
If (WP. Title =   " Image Sharing " )
{
Textbox TB =   New Textbox ();
WP. Controls. Add (TB );

spfieldcollection docfields = spw. lists [ " Image Sharing " ]. fields;
docfields. add ( " image nature 44 " , spfieldtype. text, false );

TB. Text+ =Docfields [0]. Type. tostring ();

Spview = Spw. Lists [ " Image Sharing " ]. Views [ " My test " ];
Spviewfieldcollection viewfields = View. viewfields;
Viewfields. Add ( " Image nature 44 " );
View. Update ();

// foreach (control SC in WP. controls)
// {< br> /// Search (SC, Tb);

//}
}
}
}

 

Haha, I have not sorted out the Code. There are a lot of useless code, but the function is certainly implemented. Many people understand the code, so they don't need to read the code first! There are many webparts on my site. I want to modify only some items for one webpart. This is my purpose. Image Sharing is a picture library of mine. After I put it on the homepage, I used the image library as the webpart. What other slides, reports, and so on are there. It doesn't matter, it doesn't affect us. Compile a webpart in vs2008 and use the webpartmanager class to retrieve all the webparts on the page where the webpart is saved by hand. traverse the webpart and determine the title of the webpart to be modified, you can find the webpart you want to modify. In my example, A textbox is added to the webpart named image sharing, which is very simple. However, there are many other controls in the webpart generated by the textbox, you can traverse the controls contained in the current webpart by yourself ~!

In the code, spfieldcollection and spviewfieldcollection are used to add column for The view code in the webpart. This is because the column can be used in SharePoint site settings. If you are interested, try it, I will not go into details ~!

I am also a newbie. I have some errors and suggestions. Even if you mention the picture, I am not doing it. I will be more happy if I make it myself ~! Continue the demo ~

 

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.