Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add user control to web part in Sharepoint 2013

I've got a visual web part (created with standart Visual Stuido 2012 template from "Add new item" form) with just a single <div id="newsListDiv" runat="server"></div> element. I want to programmatically add my own user control to it using the following code:

protected void Page_Load(object sender, EventArgs e)
{    
    NewsLine newsLine = Page.LoadControl(@"~/_ControlTemplates/MainTheme/NewsLine.ascx") as NewsLine;
    newsListDiv.Controls.Add(newsLine);
}

But when I deploy the solution and add the web part to the page it shows an error page, telling me that the file '/_ControlTemplates/MainTheme/NewsLine.ascx' does not exist. But if I look into folder "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\CONTROLTEMPLATES\MainTheme" I can see that the file is present there. I've tried to set the trust level to "Full" but got the same error. I also tried adding the user control in .ascx file like this:

<%@ Register Src="~/_controltemplates/MainTheme/NewsLine.ascx" TagPrefix="uc1" TagName="NewsLine" %>

<div id="newsListDiv" runat="server">
    <uc1:NewsLine runat="server" id="NewsLine" />
</div>

And that way a get a compilation error: "The name 'InitializeControl' does not exist in the current context". I've also noticed, that as soon as I add Register or Reference line (with the path to my user control) to my .ascx file, the .g.cs file becomes blank! And it fills up again when I remove that line. I tried many different path writings like "../_controltemplates/", "/controltemplates/15/", etc. But none of them made any difference. I'm getting desperate here, please help!

like image 349
4tuneTeller Avatar asked Jan 10 '13 10:01

4tuneTeller


People also ask

How do I add a user control to a SharePoint Webpart?

In the list of SharePoint templates, choose User Control (Farm Solution Only). User controls work only in farm solutions. In the Name box, specify a name for the user control, and then choose the Add button.

How do you deploy a user control in SharePoint 2013?

Expand Office/SharePoint node and then choose SharePoint Solutions. Choose the SharePoint 2013 – Empty Project template. Name the project as CustomControlProject. Choose the "Deploy as a farm solution" option (because User Control will work only in farm solutions) and click Finish.

How do I create a custom control in SharePoint?

Double-click the project . cs or . vb file in Solution Explorer, and add code such as in the previous example to define a class for a custom control that extends a SharePoint Foundation control. Press CTRL+SHIFT+B to build the solution.


1 Answers

You forgot to try one more option. It's the same when accessing the _layouts folder. You should specify the 15 hive.

The correct path is "~/_ControlTemplates/15

 NewsLine newsLine = Page.LoadControl(@"~/_ControlTemplates/15/MainTheme/NewsLine.ascx") 
like image 188
Rick Avatar answered Jan 04 '23 08:01

Rick