Need some help with sharepoint web part development - sharepoint

I have a custom web part where there is a text box. My custom web part also has a custom setting where user enters some values separated by commas. So on load, the UI should display the values entered in the setting (without commas though).
Is this possible with normal ascx user control, since it cannot access the sharepoint web part property at the time of creation and hence cannot display the message on load.
Need some help to solve this.
Thanx

I assumed you're probably doing something like this.
What you'll need to do is pass your web part properties to your usercontrol. You can do this by creating public properties in your usercontrol class (demonstrated in the link I give above).
Code sample from the article:
// Loads a user control
MyUserControl myUserControl = (MyUserControl)Page.LoadControl("~/_controltemplates/MyWebPart/MyUserControl.ascx");
myUserControl.Web = SPContext.Current.Web;
myUserControl.TextColor = this.TextColor;

Related

SharePoint WebPart zoneId

I am using SharePoint online.
I want to use this CSOM code to add a WebPart to a page:
SP.File oFile = _web.GetFileByUrl(SiteUrl + "/SitePages/" + pageName);
oFile.CheckOut();
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
var importedWebPart = limitedWebPartManager.ImportWebPart(webPartSchemaXml);
var webPart = limitedWebPartManager.AddWebPart(importedWebPart.WebPart, zoneid, zoneIndex);
oFile.Update();
await SiteCtx.ExecuteQueryAsync();
oFile.CheckIn(String.Empty, CheckinType.MinorCheckIn);
The problem is how to assign correct values to the zoneid string variable,
which is the name of the Web Part zone to which to add the Web Part.
When I run this code nothing happens!
(it doesn't add the WebPart to the page and I am suspecting it is related to the wrong zoneId).
I have read various post, ranging from accessing the code behind of the .aspx page trying to find the WebPartZone, accessing the WebPartManager class (which should list the ZoneId's but I don't know how to get it, since that I am using the LimitedWebPartManager class).
I have tried various values for zoneId, but at the moment none of them work:
Zone 1 (just a guess!)
Zone 2 (i see it in the right tab when manually editing the webpart through Edit page)
Body (with this the code worked some days ago! but now it doesn't anymore)
Header
Left
Bottom
What is the proper method of findind zoneId's?
EDIT
The page is the homepage, I have read somewhere that it is a wiki page so maybe it has different ZoneId's.
ZoneIDs might be different depending on the page layout, but usually out-of-the-box SharePoint layouts use the following ID's for webpart zones:
TitleBar
Header
LeftColumn
MiddleColumn
RightColumn
Footer
Make sure that you invoke the update() method on your file object and executeQueryAsync() method on your context after importing the webpart - the latter function especially is responsible for sending the request to server and applying your changes.
Here's a nice article about adding webparts to pages programatically: How to programmatically add a ClientSide Web Part to a SharePoint page

Kentico 10 Controlling visibility of webpart based on another webpart

I have 2 editable text web parts on a page. One web part has default text set on it as the majority of the time it will be the same text.
I want to set visibility of this web part based on the text of another web part for the live site.
I tried this:
{#!string.IsNullOrEmpty(WebPart.GetValue("AreaDescription", "Content")) &&
CMS.PortalEngine.ViewModeEnum.LiveSite == CMS.PortalEngine.PortalContext.ViewMode #}
But then it's never visible so it's not picking up the text inside the other webpart successfully. So I'm thinking maybe I need to call GetContent() to get the user provided text of the editable region. I tried writing a custom transformation method but using the below - PagePlaceholder is unknown and I'm not sure how to get a reference to it.
CMSAbstractWebPart webpart = PagePlaceholder.FindWebPart("webPartId");
Try {% (ViewMode == "LiveSite") && (CMSContext.CurrentDocument.WebPartID != "") #%}
in a macro to get the content of editable text:
{%CurrentDocument.DocumentContent["webPartId"]#%}
so what you are looking for is something like this:
{%!string.IsNullOrEmpty(CurrentDocument.DocumentContent["AreaDescription"]) && (PortalContext.ViewMode == "LiveSite")#%}
From Kentico support - macros will not allow me to do what I want to achieve because content of other web parts is not available in CMSContext or in DocumentContent.
As I was trying to avoid custom web parts they suggested I could create an alternative layout for my web part that would be the same as default plus add:
your handler for onLoad, or any better event
If it was a custom web part I could add:
///find another web part - in this case WebPart1
CMSAbstractWebPart webpart1 = PagePlaceholder.FindWebPart("WebPart1");
///store value/content of chosen property
string wp1DefaultText = ValidationHelper.GetString(webpart1.GetValue("DefaultText"), "");
Potentially on Prerender to ensure it wouldn't be overridden.
At this stage I'm just going to remove the default text as it feels like I'm fighting the system and the value gained probably isn't worth the effort/customisation.

Proper Kentico object for non-user-editable block of static html/javascript

The Situation: I've got a mid-sized chunk of html/javascript that contains an authentication script/input (it's a text input, radio control, and a combo box and a few buttons). What it is is less important than the concept that it's a mass of static client side code that the marketing department can pretty easily accidentally the whole thing.
The Desire: I want the users to be able to add it as a whole to a page, but not be able to modify it. When something needs to change, I want to change it in one place and have it be changed on all the pages.
What I've Tried: Widget with a default text. It works, but feels wrong. Users can edit it, and if they do when I fix it one place it doesn't propagate to all the instances. I'm a bit of a Kentico noob, but it seems like there should be a better way to do this.
Also note: I'm using portal engine if that makes a difference.
A widget is the proper usage. What you make your widget inherit from is the key in this case. I'd suggest creating a new widget based on a static HTML webpart. This way you can set the static HTML markup and hide the property from the content editor on the front end. You can do this by going to the Properties tab of the widget and setting the visibility of the field on the form. Don't delete the field, just hide it. It should be a checkbox that says hide on public form or editing form.
** Edit **
As I read through my answer and comments, I realized I meant to say clone the static HTML webpart and set its default text to your javascript. Then create a widget based on that cloned webpart. The text will reside in the web part and will allow you to update it in one place later, if needed.
I will not do it this way because you will be not able to make changes in the future. You can better create a new webpart this can be an empty webpart and then create a custom layout. In this layout you can put you're code. In this way you can always change you're code in the future and then it will be changed on all the places where the widget is placed.
I'd use a new widget based on the Static HTML webpart (make the field read only or hide it as Brenden mentioned), but store the data in a new custom setting.
no coding needed (only a macro to read the custom setting)
able to edit the script on the fly on any instance in the settings module. If you have multiple of these settings you won't need to go through all kinds of widgets to adjust their default setting but find them on a central place.
Cheers!
David
In this case I think it makes sense to create a custom web part to store all your code in it and use it that way. If you want to achieve it without creating a custom web part, you have to store the code in some non-web part and not widget specific object. I like the suggestion of creating a custom setting. You can then access this custom setting via a macro. This macro can be used as a default property of a newly created web part (inherited e.g. from the static text web part, you'd use the text property). You may as well create a widget out of it. Another approach is to use Kentico localization keys as a workaround. you can create a key in the Localization application and access it again, via a macro, e.g. {?customkey.myhtml?}. The approach with a custom setting however sounds cleaner to me.
This syntax should be working to access a custom setting value via macro:
{%Settings.CustomSettings.xxx%}
{%Settings.CustomSettings["xxx"]%}
{%Settings.CustomSettings.GetValue("xxx")%}

Opening different xpages forms from a view panel

I have an Xpages application that pulls data from another .nsf file. I have a view panel linked to a view in that db. The view has documents with several different forms in it. I want to be able to open each document in it's own form(xpage).
How do I write a computed At Runtime, open selected document using: statement that will select the correct Xpage to present the document.
If you use the Data View component instead of a View Panel, you can compute the pageName attribute, referencing the var attribute to return a different value for each row based on the document that row represents. The flexibility of the Data View component also makes it easier to make your app look more like a modern web application and less like an Excel spreadsheet. As an additional bonus, the mobile theme invokes a renderer that makes each Data View instance look like a native mobile list, so using Data Views instead of View Panels simplifies mobile development.
You have 2 options:
use "use xpage associated with form" and edit the form's property
use a SSJS formula to compute the Form. You provide a variable name in the view control var to access a view row as XSPViewEntry. If the Form is in a view column even one you don't display you use .getColumnValue otherwise getDocument.getItemValueString
Does that work for you?
Maybe this mothed can help you: Unable to get document page name for
Hope this helps
Mark
I had a similar problem today. I use only one form but 3 different xpages for associated with this form. I have 3 different document types in the view. I used rowData the get the type of the document.
try{
var v=rowData.getColumnValue("form");
if(v.indexOf("x")> -1){var page ="x.xsp"}
else if(v.indexOf("y") > -1){var page = "y.xsp"}
else{var page = "z.xsp"}
}catch(e){
var page = "x.xsp"
}
So to your view you can create a column with the value of the form and you can use it.
I have used the extension library Dynamic View control which has an event you can code to get a handle to the NotesViewEntry which was selected. See the demo database page Domino_DynamicView.xsp and the Custom Event Handler tab for an example.
Note, in 8.5.3 (I have not upgraded yet) if you add or edit the eventHandler for onColumnClick it will be added to the XPages source as an xe:eventHandler. It needs to be an xp:eventHandler to work. The way to do it is to copy the code in the source from the exiting event and delete it. Recreate the event and update the code. Then go back into the source and change the tags within the eventHandler to xp:.

SharePoint:FormField in a custom webpart?

On my home page, I want a simple webpart that allow users to quickly fill an entry in a list.
The list have, let's say, three fields : title (text), body (rich text), category (lookup).
I don't want to use the standard DataFormWebPart because I have a bit of code-behind that also fill some technical hidden fields of my list (actually, I don't exclude the DataFormWebPart, but I didn't find how to use it with code behind).
So I started to implement a custom webpart. Because I don't want to have to handle manually each field input, I started to use the FormField control, which automatically choose the rendering control, and provide a Value property with the correct format :
<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" />
This code is not sufficient, I have to specified the listid :
<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" ListId="{title list guid}" />
This is working quite correctly. I can in code access the fldTitle.Value to retrieve the user input.
BUT I have to include the webpart in a properly packaged and deployed feature, that can be activated. The webpart will always target the same list, but as the list is also instanciated in the feature (ListInstance element), I can't know the Guid in advance.
I've tried using several technics to set the list ID on the fly, but without success.
I've also "reflectored" the SP dlls to notice FormComponent class are using a "Context" that is set by ListFormWebPart.
Finally, my questions are :
is it the correct way to create a custom input webpart on the home page (not a list custom form) ?
how can I keep the behavior of the FormField (choose the right control and handle the input and its conversion to the storage format) ?
Will I have to create a custom ListFormWebPart ?
May I play with ControlTemplates ?
thanks in advance for the help... I'm struggling with this simple case for days now...
I think that customizing form templates is the easiest way to customize list forms. Since custom form templates are implemented as user controls you can add whatever code you want.
See the following article:
http://www.codeproject.com/KB/sharepoint/SharePointListForms.aspx

Resources