How can I add WebPart on Sharepoint WikiPage? - c#

How can I add WebPart on Sharepoint WikiPage?
For example:
How can I add GettingStartedWebPart on Sharepoint 2013 home page (SitePages/Home.aspx)?
I used "Team Site" template for my Site Collection. And removed default
"Get started with your site" web part.
I am trying:
...
SPLimitedWebPartManager spLimitedWebPartManager = spFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
var gettingStartedWebPart = new GettingStartedWebPart();
....
spLimitedWebPartManager.AddWebPart(gettingStartedWebPart, "wpz", 0);
But it doesn't work. Do you have any idea how to do it? Thank you for your time :) and solutions.
Edit:
I found the solution. First of all, you can use standard method for "Wiki Page" WikiEditPage.InsertWebPartIntoWikiPage. But, this method doesn't work correctly... More of this and one of the possible fix you can find if you read this article about SharePoint 2010 (The realization of WikiEditPage.InsertWebPartIntoWikiPage in SharePoint 2013 is the same as in SharePoint 2010): http://habaneroconsulting.com/insights/programmatically-change-content-on-a-wiki-page-in-sharepoint-2010#.VgAEAfmqpBd.
Also: http://maxshulga.blogspot.com/2010/06/wikieditpageinsertwebpartintowikipage.html

I've done something like this in the past with
private void AddWebPartToPage(SPWeb web, string filepath, string zoneidname, int zoneIndex)
{
SPLimitedWebPartManager wpm = web.GetLimitedWebPartManager(filepath, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
wpm.AddWebPart(wp, zoneidname, zoneIndex);
}
AddWebPartToPage(web, web.Url + "SitePages/Home.aspx", "CentreLeft", 0);
You will need to test this though.
Hope this helps.
Cheers
Truez

Related

How to hide sharepoint document library

I know that document libraries in sharepoint have flag Hidden. Here you can find some documentation about it.
But i want to hide some libraries from libraries list in my application. So i want to set this flag to 'true'. How i can do that on my sharepoint team site? Maybe some permission changes?
Hiding from certain users? then you should go with the permission settings in that document lib.
I'm not sure if you can do that through UI but you can definitely do that using SharePoint Designer. Just open your web site in SharePoint Designer navigate to "Lists and libraries", select your library and in "General settings" set flag "Hide from browser" and press "Save".
If I understood question correctly, do the following:
string url = "https://contoso.com:666/sites/SiteName/WebName";
Guid listGuid = new Guid(...);
using(SPSite site = new SPSite(url))
{
using(SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listGuid];
list.Hidden = true;
list.Update();
web.AllowUnsafeUpadtes = false;
}
}
Haven't tested the code, but I believe this should work.

Sharepoint - upgrade webpart properties

I'm playing with SharePoint 2010 now and have a problem.
There is a feature that is responsible for webparts - it's scope is web. It's needed to update some properties of already created webparts, for example - title.
So I've overridden FeatureUpgrading event and added custom upgrade action into feature manifest - there is no problem here.
In that feature receiver I plan to have a code that should get the file with needed page, check it out, iterate through all the web parts on it, change property and then check in page back.
The problem is that all my webparts appear as ErrorWebPart with empty title.
By the way, if I use the same code in FeatureDeactivating event - everything works good.
The only difference I've noticed - in featureupgrading HttpContext.Current was null. So I've populated it manually but it didn't help.
While googling, the only two advices were: populate HttpContext and ensure that libs with webparts are in GAC. Both conditions are done in my situation.
The sample code from FeatureUpgrading is as proper:
SPUtility.ValidateFormDigest();
web.AllowUnsafeUpdates = true;
var request = new HttpRequest("", web.Url, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
var fileUrl = web.Url + "/pages/sample.aspx";
var file = web.GetFile(fileUrl);
if (file.CheckOutType == SPFile.SPCheckOutType.None)
{
file.CheckOut();
}
using (var webPartsManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
foreach (WebPart webPart in webPartsManager.WebParts)
{
Console.WriteLine(webPart.GetType().ToString());
}
}
file.CheckIn("System update");
Would appreciate any help. Maybe there is something I'm missing or there is another variant to accomplish described task?
Regards.
Since it is working on Deactivating(), I think the order in which the features are activated is the problem.
You may have to ensure the below:
1. First activate the Feature that adds the webparts
2. Then activate the new feature.
You can change this in package. Or better add a dependency to your new feature.

ExchangeServiceBinding namespace error

Summary: application will not accept the ExchangeServiceBinding command.
Details:
I am trying to loop through a very large mailbox, so I am using an index to break the inbox into 200 email chunks. The only example I could find (shown below) keeps returning
the type or namespace name “ExchangeServiceBinding” could not be found (are you missing a using directive or an assembly reference? )
Which I find strange, because I am using it using Microsoft.Exchange.WebServices;. Any ideas or help is greatly appreciated. I am running Windows 7 and Visual Studio 2010 and trying to access Exchange 2007 mailboxes.
Things I've tried:
searching Google
searching Stack Overflow
searching MSDN
slamming my head on my desk
trial and error
Code:
// Create binding variable to be used for GetItemsFromInbox().
// Set up the binding with credentials and URL.
ExchangeServiceBinding binding = new ExchangeServiceBinding();
binding.Credentials = new NetworkCredential(dUser, dPassword, dDomain);
binding.Url = new Uri("https://" + ExchangeServerName + "/EWS/Exchange.asmx");
// Set up the binding for Exchange impersonation.
binding.ExchangeImpersonation = new ExchangeImpersonationType();
binding.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType();
binding.ExchangeImpersonation.ConnectingSID.PrimarySmtpAddress = "mailboxnamehere”;
// Call GetItemsFromInbox()
int index = 0;
bool looping = true;
while (looping)
{
List<ItemType> items = GetItemsFromInbox(binding, index, 200, index);
if (items == null || items.count == 0)
{
looping = false;
break;
}
// Do your work here
}
Instead of the Exchange Web Services, use the Exchange Managed API.
SDK: http://msdn.microsoft.com/en-us/library/dd633710(v=exchg.80).aspx
Download: http://www.microsoft.com/download/en/details.aspx?id=13480
It's much easier to use than the WebServices.
I found my error. This methodology only works for Exchange 2010. Since I am running Exchange 2007 I will have to figure out a completely different way to make this work.
Thank you everyone for you help, I really appreciate it.
You should add a WebReference to your solution to the exchange WebService.
https://exchaneServerName/EWS/Exchange.asmx
ExchangeServiceBinding is contained into the ews.dll. According to your error, you didn't add a reference to this DLL file.
More information about Generating Exchange Web Services Proxy Classes:
So now you have a code file with the autogenerated proxies. Next, you compile your code file into an assembly for use in your Exchange Web Services projects. The C# compiler is available with the Visual Studio 2005 Command Prompt. Assuming that you named your code file EWS.cs, you can run the following command at the command prompt to compile your code into an assembly:
csc /target:library /out:EWS.dll EWS.cs
Notice that EWS.dll is the name of the compiled assembly. This is how EWS.dll is created.

How Can I Add a New Column to All Document Library Lists in SharePoint 2010 Using Visual Studio 2010?

I can't figure out how to create a solution in Visual Studio 2010 that will allow me to alter existing lists in a SharePoint 2010 site. Specifically, I want to add a new column, which contains a small icon, to existing document library lists. I want to be able to take an action when someone clicks on one of the new icons. I also want this new column to become part of the default view for new document library lists. All of this needs to be easily deployed to a SharePoint 2010 site via a .wsp file.
Extensive searching on Google has shown how to create new lists and new column types, and how to programmatically add columns to one of the new lists, but not how to modify all existing lists.
I'm brand new to SharePoint, and any pointers towards a solution would be much appreciated. Thanks!
If you really want to change all Document Libraries in the site, you could try changing the Content Type:
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType ct = web.ContentTypes[SPBuiltInContentTypeId.Document];
SPField fld = web.Fields.GetField(fldName);
SPFieldLink lnk = new SPFieldLink(fld);
ct.FieldLinks.Add(lnk);
ct.Update(true);
}
}
The above code is shortened and modified from the example of SPContentType.Update Method (Boolean). The MSDN article also has good general information on Updating Content Types.

how to add a WebPart to all pages in a SharePoint site?

I am using SharePiont Server 2007 Enterprise with Windows Server 2008 Enterprise, and I am using publishing portal template. I am developing using VSTS 2008 + C# + .Net 3.5. I want to know how to add a WebPart to all pages of a SharePoint Site? Any reference samples?
I want to use this WebPart to display some common information (but the information may change dynamically, and it is why I choose a WebPart) on all pages.
There are two ways to do this depending on your situation.
If the sites exist already, you need to iterate over the sites, adding the web part:
http://blogs.msdn.com/tconte/archive/2007/01/18/programmatically-adding-web-parts-to-a-page.aspx
If the sites do not exist then you can add the web part to the site template:
How to add a web part page to a site definition?
Here's the code from Shiraz's first link worked out a bit more:
(Note: This code is not optimized, for instance, looping through a List's Items collection is not something you should normally do, but seeing as this is probably a one time action there's no problem)
private void AddCustomWebPartToAllPages()
{
using(SPSite site = new SPSite("http://sharepoint"))
{
GetWebsRecursively(site.OpenWeb());
}
}
private void GetWebsRecursively(SPWeb web)
{
//loop through all pages in the SPWeb's Pages library
foreach(var item in web.Lists["Pages"].Items)
{
SPFile f = item.File;
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
//ADD YOUR WEBPART
YourCustomWebPart wp = new YourCustomWebPart();
wp.YourCustomWebPartProperty = propertyValue;
wpm.AddWebPart(wp, "ZONEID", 1);
f.Publish("Added Web Part");
f.Approve("Web Part addition approved");
}
// now do this recursively
foreach(var subWeb in web.Webs)
{
GetWebsRecursively(subWeb);
}
web.Dispose();
}

Categories

Resources