Find out where a web part is being used? - sharepoint

Is there an easy way to find out which sites/pages are using a particular web part in SharePoint 2007?

I think the only option would be to write a small tool that iterates over all web sites and pages and checks whether the web part is placed there.

If you know your site and page you can find all webparts of page and check if your webpart is there:
string AbsolutePageUrl = "http://YourSite/Page.aspx";
using (SPSite site = new SPSite(AbsolutePageUrl))
{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (var webpart in SpWebPartManger.WebParts)
{
if (webpart is MyWebPart)
//your web part is here
}
}
}
but if you want to check this in all sites you have to find all sites and all pages to check for your webpart Existence

Related

How do I obtain Central Administration site url programmatically?

I have some code (console app) running on a SharePoint farm machine, and I need the app to figure out the url of Central Administration site for that farm. I remember seeing some SharePoint API doing exactly that, but I can't find it now.
I've seen a bunch of hacks people are using for that, like looking it up in Windows registry, but I need a way via SharePoint API.
in C#
Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb =
SPAdministrationWebApplication.Local;
To expand on the answer from #RDeVaney:
Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb =
SPAdministrationWebApplication.Local;
string centralAdminUrl = centralWeb.Sites[0].Url;
Here is the code from msdn, please refer if it can answer your question
SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local);
foreach (SPWebService webService in webServices)
{
foreach (SPWebApplication webApp in webService.WebApplications)
{
if (!webApp.IsAdministrationWebApplication)
{
get the URL here
}
}
}

How to get Site name dynamically

I want to Change Title and No of Links in Recent Changes of Sharepoint 2010's Wiki page library.
I made one custom user control for that. It work fine but there is one problem that I am unable to pass the url of site in
using (SPSite site = new SPSite(SiteCollection))
I have to pass SiteCollection Statically. I want to make it dynamic. Mine site is a sub site. Is it possible to get Site link to open the Web..
Thanks
just use:
SPSite site = SPContext.Current.Site
the same goes for using a subsite:
SPWeb web = SPContext.Current.Web
No need for using statements if you use the context, you dont need to dispose it :)
Try using this : SPContext.Current.Web.Site.Url.
Nipesh,
You can getting all Webs using below code
SPSite rootSite = SPContext.Current.Site;
foreach (SPWeb oWeb in rootSite.AllWebs)
{
//This Using for SPSite is optional
using (SPSite site = new SPSite(oWeb.Url))
{
//This Using for SPWeb is optional
using (SPWeb web = site.OpenWeb())
{
//You can get all web in this object & perform operation
}
}
oWeb.Dispose();
}
Above both Using for SPSite & SPWeb are optional you can direct access SPWeb object from oWeb object.
Happy Coding...!!!

Collaboration portal approval workflow

The company I work for has recently taken over a SharePoint 2007 project form another company. The other company created a site using the Collaboration Portal Publishing template. Since this is an internet web site, this is causing me a couple of problems.
By default the approval workflow is not activated on the Pages Libraries of the site, and the client requires the workflow to be active on all of the Pages Libraries. The problem is that the site is massive, and doing this manually will take too long, and because it’s such a large site I can’t recreate it from scratch.
Is there a way I can activate the approval workflow on all the Pages Libraries of the site? Could I maybe change something in the site definition? Or is there a way to activate it programmatically? Then I could create a console app that will recursively iterate through the sites and attach the workflow to the Pages Libraries?
Your can try with this code :
using (MossFramework.DocumentLibraryHelper docLibraryHelper = new MossFramework.DocumentLibraryHelper("SITENAME"))
{
using (SPSite site = new SPSite(docLibraryHelper.MossSiteAddress))
{
SPWeb siteCollection = site.OpenWeb();
siteCollection.AllowUnsafeUpdates = true;
SPFolder folder = siteCollection.GetFolder("Document Centre");
SPList list = siteCollection.Lists["Document Centre"];
Guid wfBaseId = new Guid("{5703E6AC-1C65-440F-88DC-EB65F2C6DF82}");
SPWorkflowAssociation wrkFl = list.WorkflowAssociations.GetAssociationByBaseID(wfBaseId);
foreach (SPListItem spListItem in list.Items)
{
site.WorkflowManager.StartWorkflow(spListItem, wrkFl, wrkFl.AssociationData);
// Error occurs here
}
site.Close();
}
}
or
You can try with this
I hope that helps

Deploy two connected webparts in a page layout during feature activation?

I've implemented 2 webparts (deriving from Microsoft.SharePoint.WebPartPages.WebPart, the WSS 3 WebPart), one of which is a provider and the other the consumer (implementing ASP.net connection model, with ConnectionProviderAttribute and ConnectionConsumerAttribute methods).
I managed to deploy them in a feature which also deploys a Page Layout containing two webpart zones, which are themselves populated during the FeatureAvtivated method of the feature receiver, with the 2 newly created webparts. All of this works just fine.
For information, I used this link to make it work. Beware, the method using AllUsersWebPart tag in elements.xml, shown in links like this one (http://www.andrewconnell.com/blog/archive/2007/10/07/Having-Default-Web-Parts-in-new-Pages-Based-Off-Page.aspx) work, but if you deactivate, then reactivate your feature, you just have double webparts in your future pages based on the layout. The method described here (http://sharepoint.coultress.com/2008/06/adding-web-part-to-page-layout.html) just threw me an error when analysing metadata for the layout aspx file (the problem seemed to come from the line in the ZoneTemplate tag).
My next goal is to connect these webparts together right after all this, thus enabling the end user to create pages, based on the layout, containing by default the two webparts connected together (right now everything works except for the connected part).
I tried something like this, using ASP.net connection model (the other one, WSS model, throws logically an error because I'm not implementing the good interfaces). But even though the connection resulting from the "mgr.SPConnectWebParts()" method doesn't throw any exception and actually adds the connection to the connection list of the webpart manager, I can see in debug mode that the connection property 'IsActive" is false (maybe normal), and that when I create a new page based on the layout, the webparts appear not connected.
Any guess? I believe there's something with the fact that the webparts cannot be connected before the page containing them is actually created, but I'm far from sure of it.
Declarative web part connection provisioning is actually quite straightforward:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Module1">
<File Path="Module1\default.aspx" Url="demo.aspx">
<AllUsersWebPart ID="testProvider">...</AllUsersWebPart>
<AllUsersWebPart ID="testConsumer">...</AllUsersWebPart>
<WebPartConnection ID="testConnection"
ProviderID="testProvider"
ProviderConnectionPointID="providerID"
ConsumerID="testConsumer"
ConsumerConnectionPointID="consumerID" />
</File>
</Module>
</Elements>
Details:
http://blogs.code-counsel.net/Wouter/Lists/Posts/Post.aspx?ID=161
You can find connection point IDs with PowerShell if you first connect your web parts manually:
$web = Get-SPWeb <WebURL>
$wpman = $web.GetLimitedWebPartManager("<PageURL>", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wpman.SPWebPartConnections
Tried creating the web parts on the page programmatically? You'll have far fewer headaches than trying to do it declaratively.
Look up the SPLimitedWebPartManager class for how to handle web parts on a provisioned page.
Also, web parts in a web part zone are tied to the URL of the page on which they are added. This is by design of the the ASP.NET Web Part Manager.
Thus, if you added web parts to zones on a page layout at directory: http://webapp/sites/site/_catalog/master/mypagelayout.aspx - the web parts will ONLY appear on that page. Createa new page at /sites/site/Pages/MyPage.aspx and the web parts you added before won't appear. The workaround for this is to explicitly add web parts not within web part zones, and this can only be done in an authored page layout (usually in SharePoint Designer).
If the web parts are static in the page layout (and you want them to show in every page) then this is actually easier for you to deploy - just maintain the layout in your source, and have it provisioned via a Module element.
Finally I used another approach to reach my goal. In the OnLoad event of the provider webpart, I check if my page is in edit/new mode, and then check if the page contains the consumer webpart (via the webpartmanager) and if they are not already connected. If this is the case, I connect them.
The code to connect permanently the webparts:
private void SetUpConnections()
{
SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite siteContext = new SPSite(SPContext.Current.Site.ID))
using (SPWeb webContext = siteContext.OpenWeb(siteContext.ServerRelativeUrl))
using (SPLimitedWebPartManager spManager = webContext.GetFile(SPContext.Current.File.Url).GetLimitedWebPartManager(PersonalizationScope.Shared))
{
foreach (Microsoft.SharePoint.WebPartPages.WebPart consumer in spManager.WebParts)
{
if (consumer is MyConsumerWebPart)
{
bool alreadyConnected = false;
Microsoft.SharePoint.WebPartPages.WebPart provider = spManager.WebParts[this.ID] as Microsoft.SharePoint.WebPartPages.WebPart;
foreach (SPWebPartConnection connection in spManager.SPWebPartConnections)
{
if (connection.Provider == provider && connection.Consumer == consumer) { alreadyConnected = true; break; }
}
if (!alreadyConnected)
{
// Connects webparts permanently (but the page would need a reload to display the connection)
ProviderConnectionPoint providerConnectionPoint = spManager.GetProviderConnectionPoints(provider)["MyConnectionProviderInterfaceId"];
ConsumerConnectionPoint consumerConnectionPoint = spManager.GetConsumerConnectionPoints(consumer)["MyConnectionConsumerInterfaceId"];
spManager.SPConnectWebParts(provider, providerConnectionPoint, consumer, consumerConnectionPoint);
// Connects webparts locally (for current edit mode)
SPWebPartManager currentSPManager = WebPartManager.GetCurrentWebPartManager(this.Page) as SPWebPartManager;
System.Web.UI.WebControls.WebParts.WebPart currentProvider = this;
System.Web.UI.WebControls.WebParts.WebPart currentConsumer = currentSPManager.WebParts[consumer.ID];
ProviderConnectionPoint currentProviderConnectionPoint = currentSPManager.GetProviderConnectionPoints(currentProvider)["SearchBarProvider"];
ConsumerConnectionPoint currentConsumerConnectionPoint = currentSPManager.GetConsumerConnectionPoints(currentConsumer)["SearchBarConsumer"];
currentSPManager.SPConnectWebParts(currentProvider, currentProviderConnectionPoint, currentConsumer, currentConsumerConnectionPoint);
}
}
}
}
});
}
The code to check if the page is in new/edit mode:
if (SPContext.Current.FormContext.FormMode == SPControlMode.New
|| SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
{
this.SetUpConnections();
}

How to programmatically update content in a SharePoint Web Part?

Does anybody know how to programmatically update the content of any of the standard SharePoint v3 Web Parts?
As an example, put a Link Summary Web Part on a page. Add some links to it. Now, how can I update this information using the WSS API?
I have not found any direct way to do this, my only idea so far is to export the Web Part, (then delete it), modify the generated XML, and import it back. But surely, there must be an easier way?
You can use the SPLimitedWebPartManager class to manipulate Web parts on a Web part page. An instance of this class can be obtained from an SPFile object as follows:
using (SPSite site = new SPSite("<site url>")) // e.g. http://server/sites/asite
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile("<page url>"); // e.g. /sites/asite/default.aspx
SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager();
SPLimitedWebPartCollection webParts = lwpm.WebParts;
WebPart wp = webParts[<id, index or Guid>];
// Add your code to update the Web Part
lwpm.SaveChanges(wp);
}
You can also add or delete web parts with the SPLimitedWebPartManager.
You will probably need to call SPWeb.GetWebPartCollection and use the webpart collection to mess with the WebParts thusly

Resources