Showing posts with label Sharepoint Webparts. Show all posts
Showing posts with label Sharepoint Webparts. Show all posts

Thursday, March 28, 2013

Add Multiple WebParts using PowerShell Script

Hi All,


Imagine a scenario where you need to create 100 web part pages with different templates. You would spend a lot of time creating those pages. One of the approaches can be having a PowerShell script which can create web part pages for templates that you want.

Here is a simple script that creates web part pages in a loop.

[xml]$xmlfile = Get-Content ConfigFile.xml 

foreach( $sitecoll in $xmlfile.Configuration.SiteCollection) 
{ $site = $sitecoll.name }

 $spSite= Get-SPSite $site 
$web = $spSite.OpenWeb() 
$layoutTemplate = 4 # Template code 
$web = $spSite.OpenWeb() 
$list = $web.GetList("/sites/SharePointSite/SitePages/")
 $i = 1
 while ($i -le 5)
 { Write-Host $pageTitle = "WebPartPage& + $i 

$xml = "<?xml version=""1.0"" encoding=""UTF-8""?&gt;

<Method ID=""0,NewWebPage""><SetList Scope=""Request"">" + $list.ID + "</SetList><SetVar Name=""Cmd"">NewWebPage</SetVar><SetVar Name=""ID"">New</SetVar><SetVar Name=""Type"">WebPartPage</SetVar><SetVar Name=""WebPartPageTemplate"">" + $layoutTemplate + "</SetVar><SetVar Name=""Overwrite"">true</SetVar><SetVar Name=""Title"">" + $pageTitle + "</SetVar></Method>"

 $result = $web.ProcessBatchData($xml)

 $i++ 

Write-Host -foregroundcolor Green $pageTitle 'created successfully' 

}

and when you run here is the output of the script and then final result



If you observe closely here we have specified layouttemplate, we have specified 4. This related to various templates like three column, four columns, headers the template that we select while creating web part page.

 Possible LayoutTemplate values are :

# 1 - Full Page, Vertical
# 2 - Header, Footer, 3 Columns
# 3 - Header, Left Column, Body
# 4 - Header, Right Column, Body
# 5 - Header, Footer, 2 Columns, 4 Rows
# 6 - Header, Footer, 4 Columns, Top Row
# 7 - Left Column, Header, Footer, Top Row, 3 Columns
# 8 - Right Column, Header, Footer, Top Row, 3 Columns


Sunday, January 6, 2013

How To Add Custom Web Part Properties To Visual Web Part In SharePoint 2010


Hey Folks,
Let’s see how we can add custom web part properties to a visual web part in SharePoint 2010.
Open Visual Studio 2010 -> File -> New -> Project -> Visual C# -> SharePoint -> 2010 and select Visual Web Part project template as below:
In SharePoint Customization Wizard, validate the SharePoint 2010 site URL as below:
Once Visual Studio 2010 open ups, it will look like below:
Open VisualWebPart1.cs file and add below code:

namespace AddCustomProperty.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @”~/_CONTROLTEMPLATES/AddCustomProperty/VisualWebPart1/VisualWebPart1UserControl.ascx”;
private string _Name;
[WebBrowsable(true), WebDisplayName("Name"), WebDescription("My Name"),
Personalizable(PersonalizationScope.Shared), Category("Custom Property"),
System.ComponentModel.DefaultValue("")]
public string Name
{
get { return _Name; }
set { _Name = value; }
}
protected override void CreateChildControls()
{
VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
control.Name = Name;
Controls.Add(control);
}
}
}

It should look like below:
Open VisualWebPart1UserControl.ascx file and add below code:
It should look like below:
Open VisualWebPart1UserControl.ascx.cs file and add below code:
namespace AddCustomProperty.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
public string Name { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
NameLiteral.Text = "Your Name is : " + this.Name;
}
}
}
It should look like below:
Deploy The Project:
Final Look:
Thanks & Have Fun!! :)

Friday, November 9, 2012

HOW TO ADD A CALENDAR LIST VIEW WEB PART TO ONET.XML

Hi All,

My requirement was to add a calendar in a provisioned page so I found there is a way to set web part properties inside a CDATA section of the view. The Views are generated from the Microsoft.SharePoint.WebPartPages.ListViewWebPart.

I hope this code will help your requirements as well.


<View List="Lists/PluginCalendar" BaseViewID="2" Type="CALENDAR"
       Scope="Recursive" RecurrenceRowset="TRUE" WebPartZoneID="TopWebZone"
       WebPartOrder="0">
       <![CDATA[
         <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
             <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
                       PublicKeyToken=71e9bce111e9429c</Assembly>
             <TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart
                       </TypeName>
             <Title>Calendar of Events</Title>
             <FrameType>None</FrameType>
         </WebPart>
       ]]>
 </View>

Cannot recognize the XML namespace of this Web Part

Hi All,


When provisioning pages (e.g. default.aspx) using a feature, you can add default webparts using <AllUsersWebPart>
Keep in mind you have two types of webparts, v2 en v3. When provisioning v2: you should use the following syntax:
<AllUsersWebPart WebPartZoneID="FooterLeft" WebPartOrder="2">                
<![CDATA[                             
<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">                  
...
</WebPart>                
]]>            
</AllUsersWebPart>
and with v3:
<AllUsersWebPart WebPartZoneID="FooterLeft" WebPartOrder="5">                
<![CDATA[                    
<webParts>                      
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">   ... 
</webPart>                    
</webParts>                
]]>            
</AllUsersWebPart>
Look at the difference in attributes of WebPart-tag AND the extra <webParts> tag in the v3-version. If you add <webParts> around the <webPart>-declaration in the v2-version, you could receive the error: Cannot recognize the XML namespace of this Web Part.