SharePoint Foundation 2013 - Master Page 404 NOT FOUND - c#

I hope this is right place to ask question. I tried use sosial msdn, but it seems that msdn forum is dead. I have small problem with Master Page. I have created Web Template and Master Page. When I create new site collection and I try access to it (http://win2008r2/sites/Test), it throws me error that: "404 NOT FOUND".
However, when I check MasterPage gallery: http://win2008r2/sites/Test/_catalogs/masterpage/Forms/AllItems.aspx
I can find my Demo.master . When I check Site Collection Features, I can see that my Master Page feature is active. I checked the logs and this was only thing what I found: Failure when fetching document. 0x80070002
I have set Web Template scope to farm and I have set Master Page scope to site.
I try to solve problem using google and I tried different guides but no I cannot get it work. Can anyone please help me?
Here is Element.xml:
<Module Name="MyMasterPage" List="116" Url="_catalogs/masterpage">
<File Path="MyMasterPage\Demo.master" Url="Demo.master" Type="GhostableInLibrary">
</File>
</Module>
Here is event receiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
{
Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/Demo.master", _web.Url));
_web.MasterUrl = _siteMaster.AbsolutePath;
_web.CustomMasterUrl = _siteMaster.AbsolutePath;
_web.Update();
}
});
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite curSite = (SPSite)properties.Feature.Parent;
SPWeb curWeb = curSite.RootWeb;
Uri masterUri = new Uri(curWeb.Url + "/_catalogs/masterpage/v4.master");
curWeb.MasterUrl = masterUri.AbsolutePath;
curWeb.CustomMasterUrl = masterUri.AbsolutePath;
curWeb.Update();
}
If you want to see more of my code to solve this problem, please feel free to ask, so I put more code up. ;)

Thank you Yevgeniy.Chernobrivets! Problem was that I had named my default.aspx to MySite.aspx and it didn't work. When I created it again and I keep default.aspx name it worked! Well now I have to find way to change that default.aspx name =)

Related

How to insert a header on SharePoint page

I have a very simple requirement but of course as is the case with CSOM it seems really difficult to achieve anything. I did have a solution for this, but now that the site template has changed (outside of my control) nothing works any more, and I have no idea how to debug this.
Requirement
Add a simple header to an existing SharePoint page.
Old solution
This used to work great. I pass in the page name, the web part xml and the details of the zone (I have no idea how I found those it seems ridiculously complicated just to insert a web part with code... why?...)
When i try it now I get
the object specified does not belong to a list
On the 2nd ExecuteQuery()
private void InsertWebPart(ClientContext clientContext, string webPartXml, string markerTemplate,
string pagePath, string zoneId, int zoneIndex, string description, string content, string fieldName)
{
var relUrl = new Uri(baseUrl).AbsolutePath;
clientContext.Credentials = credentials;
var web = clientContext.Web;
var page = clientContext.Web.GetFileByServerRelativeUrl($"{relUrl}/{pagePath}");
var webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
var importedWebPart = webPartManager.ImportWebPart(webPartXml);
var webPart = webPartManager.AddWebPart(importedWebPart.WebPart, zoneId, zoneIndex);
clientContext.Load(webPart);
clientContext.ExecuteQuery();
var marker = string.Format(markerTemplate, webPart.Id);
var item = page.ListItemAllFields;
clientContext.Load(item);
clientContext.ExecuteQuery();
item[fieldName] = marker + item[fieldName];
item.Update();
clientContext.ExecuteQuery();
}
I tried this solution but all I get is
the object specified does not belong to a list
Again... Please can someone advise?
How hard can it be to just simply add 1 header?
Check the solution below:
1.You can add "?contents=1" in the current site page, and remove the custom web part which add it using the CSOM code.
2.Open the site page using SharePoint designer, and edit in Advanced Mode.
3.Add the Header HTML code and JavaScript code under the "PlaceHolderMain" control.
Example code:
<div id="customHeader" style="height:20px;width:auto;background-color:green">Test</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ms-designer-ribbon").before($("#customHeader"));
});
</script>
4.Save the page.
If you can't use the SharePoint designer, we can also use script editor web part/content editor web part with some HTML code to achieve it.
If you want to make all the page have the custom header, we can add some code into the master page to make it works.

The .aspx extension is cut when redirecting between pages

I have a web application using web forms. I encountered an error during redirecting between pages. I'm using Response.Redirect("~/Logout.aspx") which is not working. I noticed, that the extension is missing in browser so in browser there is a http://localhost/mywebapp/Logout instead of http://localhost/mywebapp/Logout.aspx.
But when I change the redirect function to Response.Redirect("~/Logout.aspx/") so I add one slash to the end of the relative url then it is working properly. I can not find out why this behavior is. I do not think that it should work like this.
the complete code is really short:
try{
var provider = GetCurrentAuthProvider();
provider.RefreshToken();
}
catch(InvalidTokenException e)
{
Response.Redirect("~/Logout.aspx");
}
Did someone have similar issue?

redirect to a html page of other website c#

I want to redirect to another websites html page in my c# application when a user clicks on some button. I have written this
Response.Redirect("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html")
But it havent worked it is throwing path is not a valid virtual path
I am calling the below method in page load
Response.Redirect("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html")
and in
public void Application_BeginRequest()
{
var MyUri = Context.Request.Url;
if(MyUri.Contains("shop/wholesale") {
var String = MyUri.ToString();
Context.RewritePath("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html");
}
}
Exception:
Invalid path for child request '
http:/www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html'.
A virtual path is expected.
I've tried your code, you should change 'Context.RewritePath' to 'Response.Redirect'. It should works.
Look here, it maybe useful for you: http://www.techrepublic.com/blog/software-engineer/two-approaches-to-redirection-in-aspnet/
Also, maybe you are marketer and you are trying to promote 'fashionunic' shop :-D

Set Welcome Page - Office365 CSOM and C#

I'm wanting to set the Welcome Page for my Sharepoint site using CSOM in C#.
Essentially what I'm doing is uploading my new welcome page (home.aspx) to either Site Pages or Pages and then I want to change the welcome page to match my newly uploaded page location.
I've looked around but a lot of what I've seen is either PowerShell or Server side code.
If someone can point me in the right direction I would very much appreciate it.
How to set Welcome Page via SharePoint CSOM
public static void SetWelcomePage(Web web,string pageUrl)
{
var ctx = web.Context;
var rootFolder = web.RootFolder;
rootFolder.WelcomePage = pageUrl;
rootFolder.Update();
ctx.ExecuteQuery();
}
Usage
using (var ctx = new ClientContext(webUri))
{
SetWelcomePage(ctx.Web, "SitePages/default.aspx");
}

Modify the content of the home page programmatically in SharePoint 2010

First of all, I'm pretty new to SharePoint so don't shoot me if this is a noobish question or if I don't provide all the right information at once... :)
I have a team-site in SharePoint with following URL: "http://myServer/Opdracht_Ben/". By going to this URL I'm redirected to following page: "http://myServer/sites/Opdracht_Ben/SitePages/Home.aspx".
In Visual Studio I have a project for this site with a feature. When this site-feature is activated it should change the content of the home-page to some custom tekst and layout (i.e.: HTML). The content is not contained within a web part or the page is not a WikiPage, just text on a page.
I've been looking on MSDN and on several tech-sites and blogs but I have not found anything that could help me further. Does anyone know how I can 'reach' the content of the page and modify/update it?
PS:
Here on SO I have found a related question (Click for the question), but the provided solution is for when the web is a "Publishing Web", which is not the case here, so that solution won't do me any good.
Thanks in advance!
I have found out that the page IS a WikiPage after all. So I managed to change the content with following code:
using (var site = new SPSite(ApplicationResources.Url.SiteRoot))
{
using (var web = site.OpenWeb())
{
var page = web.GetFile(ApplicationResources.Url.FullDefaultPageName);
var item = page.Item;
item["Wiki Content"] = NewContent(title, text);
item.Update();
}
}
Maybe this is not the best method, so if anyone has a better and more reliable solution: feel free to correct me! ;)

Categories