Showing posts with label Custom Web Part. Show all posts
Showing posts with label Custom Web Part. Show all posts

Thursday, June 6, 2013

Deleting a WebPart from the WebParts Gallery using PowerShell

So you have a requirement to remove a webpart or multiple webparts from the webparts gallery in SharePoint and you need to do this with Windows PowerShell. One thing to remember is that the webparts gallery is the same as any other gallery in SharePoint and your task can easily be achieved using PowerShell. The below code will remove a webpart from the webparts gallery:

function RemoveWebPartsFromGallery([string]$siteUrl)
{
# an array of the custom webparts that were built;
$customWebPart = New-Object System.Collections.ObjectModel.Collection[System.String]
$customWebPart.Add("CustomWebPart")

$site = Get-SPSite $siteUrl

# the default SP type for a webpart gallery;
$wpCatalog = [Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog

$list = $site.GetCatalog($wpCatalog)

$wpID = New-Object System.Collections.ObjectModel.Collection[System.Int32]

foreach ($item in $list.items)
{
foreach ($wps in $customWebPart)
{
if ($item.Name.ToLower().Equals($wps))
{
$wpID.Add($item.ID)
continue
}
}
}

foreach ($i in $wpID)
{
$wpitem = $list.GetItemById($i)
$wpItem.Delete()
}

$list.Update()

Write-Host "WEBPARTS REMOVED FROM GALLERY" -ForegroundColor Green
}

You can check out the original script here a big thanks to Srinivas Challagolla for the script. 

The script has greatly helped me I hope that it has helped you. Happy scripting guys :-)

Wednesday, October 13, 2010

Creating Custom Web Part ToolPane

To create a custom toolpane is pretty simple, it's quite simple if you think about. All you need is an extra class that will create your toolpane.

I created a web part that had a custom toolpane that was used to display list in a site with checkboxes.

This toolpane allows users to select which list they wish to use on the site. Please see code below, and if you know of any better ways to do this please let me know:

WEB PART CODE EXAMPLE

using System;
using System.Runtime.InteropServices;
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;
using SMC.ClassLibrary;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;


namespace OM.FILTER.JQUERY
{
[Guid("67e99305-67bc-444d-9f9d-0c3c53d3dfe8")]
[CLSCompliant(false)]
public class FilterJQuery : Microsoft.SharePoint.WebPartPages.WebPart
{

public FilterJQuery()
{

}

protected override void CreateChildControls()
{
base.CreateChildControls();
}

public override ToolPart[] GetToolParts()
{
List _toolParts = null;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite _spSite = new SPSite("url"))
{
using (SPWeb _spWeb = _spSite.OpenWeb())
{
CustomToolPart _customPart = new CustomToolPart("url");
_customPart.Title = "All Lists";

_toolParts = new List(base.GetToolParts());
_toolParts.Insert(0, _customPart);
}
}
});

return _toolParts.ToArray();
}


[CLSCompliant(false)]
[WebBrowsable(false),
Personalizable(PersonalizationScope.Shared),
Category("Custom Settings")]
public string _collectionSelected
{
get;
set;
}
}
}

TOOL PANE CODE EXAMPLE

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;
using SMC.ClassLibrary;

namespace OM.FILTER.JQUERY
{
class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
private CheckBoxList _spLists = new CheckBoxList();

private CheckBoxList chkBox
{
get { return _spLists; }
set { _spLists = value; }
}


private String _SiteURL;

public CustomToolPart(String _webURL)
{
this._SiteURL = _webURL;
}

protected override void CreateChildControls()
{
base.CreateChildControls();

BindToolPart();
}

private void BindToolPart()
{
chkBox.DataSource = Helper.GetAllListTitles(_SiteURL);
chkBox.DataTextField = "Title";
chkBox.DataValueField = "ID";
chkBox.DataBind();

FilterJQuery _jQuery = (FilterJQuery)this.ParentToolPane.SelectedWebPart;

if (_jQuery._collectionSelected != null)
{
List arrayList = Helper.SplitString(_jQuery._collectionSelected);

foreach (ListItem _item in chkBox.Items)
{
for (int i = 0; i < arrayList.Count; i++)
{
if (_item.Value == arrayList[i])
{
_item.Selected = true;
}
}
}
}
this.Controls.Add(chkBox);
}

public override void ApplyChanges()
{
base.ApplyChanges();

FilterJQuery parentWebPart = (FilterJQuery)this.ParentToolPane.SelectedWebPart;

string _iList = "";

foreach (ListItem _item in chkBox.Items)
{
if (_item.Selected)
{
_iList += _item.Value + ";";
}
}
parentWebPart._collectionSelected = _iList;

chkBox.EnableViewState = true;
EnableViewState = true;
this.SaveViewState();
this.SaveControlState();
}
}
}

Hope this helped you out as it sure helped me out a whole lot, let me know if you have any questions and I will try and respond to it asap.