How might I create a SharePoint 2010 content query web part programatically and add it to a SharePoint page?
The post Adding/Removing web part in SharePoint website programmatically using C# code contains a detailed description of adding and removing Web Parts.
Here's a snippet taken from the above post adding a Web Part to a page (replace the WebPartToBeAdded pseudoclass with ContentByQueryWebPart):
using (SPSite spSiteTest = new SPSite(“SiteURL”)
{
using (SPWeb spWebTest = spSiteTest.OpenWeb())
{
SPWebPartCollection webparts = spWebTest.GetWebPartCollection("WebPageURL",Storage.Shared);
//create new webpart object
WebPartToBeAdded wpNew = new WebPartToBeAdded();
//set properties of new webpart object
wpNew.ZoneID = "WebPartZoneIDWhereWebPartIsToBeAdded";
wpNew.Title = "Web Part Title";
wpNew.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
wpNew.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
//add new webpart object to webparts collection
webparts.Add(wpNew);
//update spWeb object
spWebTest.Update();
}
}
Related
I’m using SharePoint 2010 and InfoPath 2010 on IE 11, w/Windows 7 operating system.
I have an InfoPath form that I want the user to be able to fill in the data and have the option to download a copy (with the data), as a pdf or word document – save as feature.
I see in InfoPath filler (office 2010) I can perform this “save as PDF” function but not in SharePoint 2010. Is there a setting I’m missing or do I have to go the route of extending SharePoint foundation w/ASP.net?
Thanks
Sorry, there is no native SharePoint 2010 functionality that meets your requirements. But there are several 3rd party tools you can purchase, or, of course, follow your own suggestion and pursue custom options.
I created a shim for SP2010 that will cause all Infopath forms in a sharepoint 2010 site to download, as long as the link to the form is in Sharepoint 2010. You will need jquery 1.12 for this to run.
$(function() {
if (document.getElementsByTagName('BODY')[0].innerHTML.indexOf('.xsn') != -1) {
$('a[href$=".xsn"]').each(function(index) {
var self = $(this);
var fileLocation = '';
var spDownloadsUrl = '/_layouts/download.aspx?SourceUrl=';
//GRAB LINK'S HREF LINK PATH AND URI ENCODE IT
var currentUrl = encodeURI(self.attr('href'));
//IF THE HREF IS TO A NETWORK FILE LOCATION EXIT THE PROCESS AND LEAVE IT ALONE
if (currentUrl.indexOf('file:') != -1) {
return;
}
//SHAREPOINT 2010 DOC LIST ELEMENTS HAVE INLINE JS ALTERING THE LINK BEHAVIOR, SO THEY NEED TO BE REMOVED
self.removeAttr('onclick');
self.removeAttr('onmousedown');
self.removeAttr('onfocus');
//IF THE LINK'S URL IS ABSOLUTE PATH, BUILD IT AS RELATIVE
if (currentUrl.indexOf('.com') != -1) {
var urlSplitOnDotCom = currentUrl.split('.com');
var urlAfterDotCom = urlSplitOnDotCom[1];
var urlPartsArr = urlAfterDotCom.split('/');
//REBUILD URL FROM ARRAY
var newPathname = "";
for (i = 1; i < urlPartsArr.length; i++) {
newPathname += "/";
newPathname += urlPartsArr[i];
}
fileLocation = newPathname;
} else {
fileLocation = currentUrl;
}
//ADD NEW URL TO INFOPATH FILE'S HREF ATTRIBUTE
self.attr('href', spDownloadsUrl + fileLocation);
});
}
});
I'm currently writing a site using Sitefinity CMS. Can someone please explain how to get the current dynamic content item from server side code on page_load?
I have written a user control to display a custom gallery of sliding images. There are multiple content types in my dynamic module. The user control will sit as part of the masterpage template rather than on every page. On each page load I would like to fetch the current dynamiccontent item that is associated with the page and examine whether it has a property with the name 'Gallery'. If so I would then extract the images and render them via the usercontrol.
Thanks,
Brian.
I'm assuming your images are related content. This gets every published content item of your type.
var dynamicModuleManager = DynamicModuleManager.GetManager();
var moduleType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.YOURTYPEHERE");
var dcItems = dynamicModuleManager.GetDataItems(moduleType)
.Where(l => l.Status == ContentLifecycleStatus.Master);
foreach (var dcItem in dcItems)
{
//pass the dynamic content item to a model constructor or populate here, then
// get your image this way:
var image = dcItem.GetRelatedItems<Image>("Images").SingleOrDefault();
if (image != null)
{
ImageUrl = image.MediaUrl;
}
}
How to open multiple windows from the controller?
For example, I have a controller that gets all the clients and for each client I want that his info is displayed in a different window or tab.
Is that possible using asp.net MVC 4?
I've done this exact thing, it was based on a button click which would fire multiple windows based on the parameters. Here's what you could do. Your controller would be like this...
[HttpPost]
public ActionResult CopyOpportunitySave(CopyOpportunityViewModel model)
{
ViewModel model = new ViewModel();
foreach (var url in "List object that contains URLS")
{
model.ClientURLs.Add(url);
}
return View("CopyOpportunity", model);
}
So now your Model class will have a property like
public List<string> ClientURLs{get; set;}
which is populated by all the URLs you will need to do a window.Open.
and Now in your view you can say
#foreach (var u in ViewModel.ClientURLs)
{
<script>
window.open(u);
</script>
}
just as long as the script inside the foreach loop is getting executed, it will work
This won't be possible by just using native MVC 4. However, one could think of some work-arounds which might resemble your requirements. For starters, is it necessary to actually open a 'browser tab' per client, or could this also be a tab page per client in one single HTML-document. If the latter is an option, you could take a look at different 'tab' controls being available in HTML.
If your really want a new browser window per client, my hint would be to return the list of possible clients to one HTML page and then use scripting in that HTML page to open a popup window for each client. For each popup / client an additional request needs to be send to the server.
Just make the link (<a></a>) that opens the client information have a target = "_blank":
John Smith
If you are using Html helpers it could be like this:
#Html.ActionLink("John Smith", "Details", "Clients", new {id = 1}, new { target = "_blank"})
This will open a new browser tab with the page you need.
I'm creating a new site collection where the document ID service should be activated. How can i programmatically set the prefix for the document ID service inside the new site collection?
[EDIT]
This seems to be the solution.
using (SPSite rootSite = new SPSite(URL + "sites/123021")) {
rootSite.Features.Add(new Guid("b50e3104-6812-424f-a011-cc90e6327318"));
using (SPWeb web = rootSite.OpenWeb(String.Empty, true)){
web.Properties["docid_settings_ui"] = "<?xml version=\"1.0\" encoding=\"utf-16\"?><DocIdUiSettings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Prefix>88888</Prefix><AssignmentEnabled>true</AssignmentEnabled></DocIdUiSettings>";
web.AllProperties["docid_settings_ui"] = "<?xml version=\"1.0\" encoding=\"utf-16\"?><DocIdUiSettings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Prefix>88888</Prefix><AssignmentEnabled>true</AssignmentEnabled></DocIdUiSettings>";
web.Update();
}
}
Thanks
Marco
You can change the prefix used by the document id service by setting the docid_msft_hier_siteprefix property of the RootWeb of the site collection (change it in both the Properties and AllProperties property bags.
Toby
I verified the "Answer" for SharePoint 2013, it does set the prefix in the Document ID settings page. But the timer jobs for Document ID service wont apply the new prefix. you should try this
// Set document id service prefix
DocumentId.SetDefaultProvider(rootSite);
DocumentId.EnableAssignment(rootSite, "MYPREFIX", true, true, true, false);
for more details information please see my blog post on this issue.
http://blogs.perficient.com/multi-shoring/blog/2015/01/13/set-document-id-prefix-programmatically-in-sharepoint-2013/
I have to create a SharePoint 2010 ribbon element (tabs, buttons, groups, etc.). Is there a way to create such elements via the SharePoint API without using custom actions?
Edit:
I've just found the possibilty to add elements like this (link):
SPRibbon r = SPRibbon.GetCurrent(Page);
r.RegisterDataExtension(/* XmlNode containing ribbon element code */);
Another workaround would be to append a custom web control and append dynamic controls to this "placeholder".
Is there a way to create elements without using xml nodes?
For layouts pages and webparts, you can create Ribbon without any XML, using Ribbon Utils for SharePoint 2010.
For example, for layouts page, you will need to inherit from RibbonUtils.RibbonLayoutsPage and provide your definition of a ribbon.
Code for the most simple page with custom ribbon tab & one button on it will look like this:
public partial class MyRibbonPage : RibbonLayoutsPage
{
public override TabDefinition GetTabDefinition()
{
return new TabDefinition()
{
Id = "TestRibbon",
Title = "Test",
Groups = new GroupDefinition[]
{
new GroupDefinition()
{
Id = "TestGroup",
Title = "Test group",
Template = GroupTemplateLibrary.SimpleTemplate,
Controls = new ControlDefinition[]
{
new ButtonDefinition()
{
Id = "TestButton",
Title = "Test button",
CommandJavaScript = "alert('test!');",
Image32Url = "/_layouts/images/lg_ICHLP.gif",
}
}
}
}
};
}
}
You will find more examples and very good documentation on the project page on CodePlex.
AFAIK, it is for now most simple and quick way to create ribbon programmatically for application pages & webparts.