Modify the content of the home page programmatically in SharePoint 2010 - c#

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! ;)

Related

c# HtmlAgilityPack class inside many classes, need to check if class exist

I am working on this for days without a solution.
For example, I have this link: https://www.aliexpress.com/item/32682673712.html
I am trying to check if the Buy now button disable
if I have this line inside the DOM : Buy Now
the problem is that this line inside a class that inside a class and so on...
I know there is an option to get a specific node with HtmlAgilityPack But I didn't succeed
var nodes = doc.DocumentNode.SelectNodes("//div[(#class='next-btn next-large next-btn-primary buynow disable')]/p");
but I don't get anything
I tried to get the entire dom and then search with inside but didn't succeed
var getHtmlWeb = new HtmlWeb();
var document = getHtmlWeb.Load(url);
I just got the html and not the DOM
another thing I tried to do is:
var Driver = new FirefoxDriver();
Driver.Navigate().GoToUrl(url);
string pagesource = Driver.PageSource;
and it did works! but this solution open the browser and I don't want that (I am running over many links)
Please help a frustrated guy :)
thanks.
This is happening because the buynow button is being loaded via JavaScript.
If you open network tab in chrome dev tools, you will notice that the page is making a call to some api to load the product information.
The url with json data for the product looks like this:
https://www.aliexpress.com/aeglodetailweb/api/store/header?itemId=32682673712&categoryId=100007324&sellerAdminSeq=202460364&storeNum=720855&minPrice=4.99&maxPrice=4.99&priceCurrency=USD
You will most probably have to send same headers as chrome is sending for the request in order to load the api endpoint in your app.

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?

Sharepoint and WCF interactions

I try to read and create XML files on a Sharepoint 2010.
I know that Sharepoint exposed some services used to interract with the application (like /_vti_bin/ListData.svc or /_vti_bin/Lists.asmx or /_vti_bin/Webs.asmx) but i dont know how to use them and if they can do what i want to do.
If someone has a sample to help me or a link with a comprehensive documentation, thank you to please share with us.
Thank you in advance, have a good day.
Here is a reference with examples: http://msdn.microsoft.com/en-us/library/office/aa979690(v=office.14).aspx
Have you tried to search on your own?
What particular problem do you have?
Now i'm able to do what i wanted.
Here is the solution to read XML files on SharePoint 2010 with a WCF Service :
public string GetLocalisationCollection()
{
CopySoapClient client = new CopySoapClient();
FieldInformation myFieldInfo = new FieldInformation();
FieldInformation[] myFieldInfoArray = { myFieldInfo };
byte[] myByteArray;
client.GetItem(Configuration.LocalisationUri, out myFieldInfoArray, out myByteArray);
return System.Text.Encoding.UTF8.GetString(myByteArray);
}

SharePoint Foundation 2013 - Master Page 404 NOT FOUND

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 =)

SharePoint 2007 - Update all site home pages

I am wondering if there is a way to update all site home pages to display a new custom web part.
I have a site collection with 100+ sub sites and I have created a custom web part which i want to display in all site home pages.. what would you suggest as the best way to do this, as as doing this manually will take considerable time?!
First write the code to programmatically add a webpart to a single homepage. The specifics of how to do this will vary based on how your homepage is structured, whether it's a publishing page, etc. It's most likely possible, but implementations could differ. You'll possibly be using something similar to this:
using(SPSite site = new SPSite("http://localhost"))
using(SPWeb web = site.RootWeb)
{
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("Pagees/Home.aspx"
, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
MyWebPart wp = new MyWebPart(); // your custom webpart
wp.Title = "My WebPart";
webParts.AddWebPart(wp, "Left", 0);
webParts.SaveChanges(wp);
}
There are lots of variations when searching the subject online.
Once you have that you can create either a console application or a feature to be executed on the top level site, open up each subsite, and then execute the above code.

Categories