vb.net client object model: view content from sharepoint 2010 wikipage - vb.net

On SharePoint 2010 i have many wikipages containing a table. Is it possible to view the content of that page in vb.net using the client object model?
I tried to get the file code using a stream but the text i get is a lot of asp.net code but not the html code/content from that page...
Dim fileAlgemeen As SP.File = Nothing
Dim siteUrl As String = "https://portal.xx.be/sites/kdb"
Dim ctx As New ClientContext(siteUrl)
Dim credentials As NetworkCredential = New NetworkCredential("xx", "xx")
ctx.Credentials = credentials
Dim web As Web = ctx.Web
ctx.Load(web)
ctx.ExecuteQuery()
Dim relativeUrl As String = "/sites/kdb/596/Algemeen.aspx"
Dim file As SP.File = web.GetFileByServerRelativeUrl(relativeUrl)
ctx.Load(file)
ctx.ExecuteQuery()
Dim fileRef = relativeUrl
Dim fileInfo As FileInformation = SP.File.OpenBinaryDirect(ctx, fileRef.ToString())
Using fileInfo.Stream
Using sr As StreamReader = New StreamReader(fileInfo.Stream)
Dim line As String = sr.ReadToEnd()
lbl.Text = line
End Using
End Using

There is no need to read file content since wiki content is stored in associated List Item PublishingPageContent field.
The following example demonstrates how to read and update wiki content using SharePoint CSOM (VB.NET)
Sub Main()
Const siteUrl As String = "https://contoso.sharepoint.com/kb/"
Const userName As String = "username#contoso.onmicrosoft.com"
Const password As String = "password"
Using ctx = New ClientContext(siteUrl)
Dim credentials As NetworkCredential = New NetworkCredential(userName, password)
ctx.Credentials = credentials
'1. Read Wiki content
Dim wikiFile = ctx.Web.GetFileByServerRelativeUrl("/kb/Pages/Welcome.aspx")
Dim wikiItem = wikiFile.ListItemAllFields
ctx.Load(wikiItem)
ctx.ExecuteQuery()
Console.WriteLine(wikiItem("PublishingPageContent"))
'2. Update wiki content
wikiItem("PublishingPageContent") = "<h1>Welcome to the SharePoint!</h1>"
wikiItem.Update()
ctx.ExecuteQuery()
End Using
End Sub
Key Points:
Wiki content is stored in List Item PublishingPageContent field
File.ListItemAllFields property is used to retrieve List Item
associated with File

Related

How would I retrieve certain information on a webpage using their ID value?

In vb.net, I can download a webpage as a string like this:
Using ee As New System.Net.WebClient()
Dim reply As String = ee.DownloadString("https://pastebin.com/eHcQRiff")
MessageBox.Show(reply)
End Using
Would it be possible to specify an ID tag of an item on the webpage so that the reply will only output the information inside of the code box/id tag?
Example:
The ID tag of RAW Paste Data on https://pastebin.com/eHcQRiff is id="paste_code" which includes the following text:
Test=1
Test=2
Is there anyway to get the WebClient to only output that exact same message using the ID tag (or any other method)?
You can use HtmlAgilityPack library
Dim document as HtmlAgilityPack.HtmlDocument = new HtmlAgilityPack.HtmlDocument()
document.Load(#"C:\YourDownloadedHtml.html")
Dim text as string = document.GetElementbyId("paste_code").InnerText
Some more sample code:
(Tested with HtmlAgilityPack 1.6.10.0)
Dim html As string = "<TD width=""""50%""""><DIV align=right>Name :<B> </B></DIV></TD><TD width=""""50%""""><div id='i1'>SomeText</div></TD><TR vAlign=center>"
Dim htmlDoc As HtmlDocument = New HtmlDocument
htmlDoc.LoadHtml(html) 'To load from html string directly
Dim name As String = htmlDoc.DocumentNode.SelectSingleNode("//td/div[#id='i1']").InnerText
Console.WriteLine(name)
Output:
SomeText

How to dynamically get path to HTML file in VSTO project for Outlook add-in

I am creating a simple Outlook 2016 add-in that will allow the users create a new mail message based on a HTML template.
I have created a Ribbon and added a button to it. Here is the click event handler code:
Private Sub btnCreateMail_Click(sender As Object, e As RibbonControlEventArgs) Handles btnCreateMail.Click
Dim app As Application = Globals.ThisAddIn.Application
Dim currentAccount As Account = app.Session.Accounts.Item(1)
Dim mailItem = DirectCast(app.CreateItem(OlItemType.olMailItem), MailItem)
mailItem.SendUsingAccount = currentAccount
mailItem.To = currentAccount.DisplayName
mailItem.BCC = IdentifyContacts()
mailItem.BodyFormat = OlBodyFormat.olFormatHTML
mailItem.Importance = OlImportance.olImportanceLow
mailItem.HTMLBody = GetEmailBody()
mailItem.Subject = "Subject"
mailItem.Display(False)
End Sub
GetEmailBody() function is responsible for reading the template and modify placeholders as required:
Private Function GetEmailBody() As String
Dim strEmailBody As String
Const strTemplatePath = "C:\Users\umute\template.htm"
' Read the template
Using reader As New StreamReader(strTemplatePath)
strEmailBody = reader.ReadToEnd
strEmailBody = strEmailBody.Replace("{TodaysDate}", Date.Now.ToString("dddd dd MMM yyyy"))
strEmailBody = strEmailBody.Replace("{Name}", GetCurrentUser())
strEmailBody = strEmailBody.Replace("{Greeting}", DetermineGreeting())
reader.Close()
End Using
Return strEmailBody
End Function
The above code works perfectly, however, I am not sure how to get the path to the template file dynamically, that is without entering the full path including C:\Users\ etc. This is because I would like to ship the template file with the add-in to avoid relying on the user to keep it in their local hard drive.
I know that in ASP.NET it is possible to use Server.Mappath("~/file.html") to accomplish this but I don't know the equivalent of this when writing an add-in for Outlook.
After hours of trying, I figured out that the correct way to reference a file is to add it as a resource.
I added a new item of type Resource to the project and added the html template to it. Then, accessed the file like this:
Dim strEmailBody As String = My.Resources.AllResources.Template

Sharepoint 2013 How to add properties to Document Set being created

I am using Sharepoint 2013 ECM to upload new document sets to a list programatically in VB.NET/C#
I am successfully creating the document set, but can not find any documentation on how to add the properties/metadata to that uploaded document set. The Folder the document set will upload to already has the properties pre-defined. I just need to set them.
The code below creates the new document set. But there is zero information I can find on the internet on how to add properties from this. Sharepoint 2010 libraries allow the DocumentSet.Create to contain a properties field, but 2013 does not appear to.
Dim context As ClientContext = New ClientContext("URL")
context.Credentials = New NetworkCredential("Username", "Password")
'Get the document library in which the document set has to be created
Dim list As List = context.Web.Lists.GetById(New Guid("dc9e7aa5-5ac3-499c-a967-fa8f04bf1c90"))
'Get the parent folder where the document set has to be created
Dim parentFolder As Folder = list.RootFolder
'Get the "Document Set" content type by id (Document Set content type Id : 0x0120D520) for the document library
Dim ct As ContentType = context.Web.ContentTypes.GetById("0x0120D520")
context.Load(ct)
context.ExecuteQuery()
'Create a new document set
'A new document set will be created in "Documents" library as "Test Document" under which you can add the documents
DocumentSet.Create(context, parentFolder, dsName, ct.Id)
context.ExecuteQuery()
Once the document set is created, you could set its properties via list item associated with a document set
Example
Using context = New ClientContext(webUrl)
context.Credentials = credentials
'Create a document set
Dim list As List = context.Web.Lists.GetByTitle("Documents")
Dim parentFolder As Folder = list.RootFolder
Dim ct As ContentType = context.Web.ContentTypes.GetById("0x0120D520")
context.Load(ct)
context.ExecuteQuery()
Dim result = DocumentSet.Create(context, parentFolder, dsName, ct.Id)
context.ExecuteQuery()
'Set DocSet properties
Dim docSetUrl = result.Value
Dim folder = context.Web.GetFolderByServerRelativeUrl(docSetUrl)
folder.ListItemAllFields("DocumentSetDescription") = "Orders 2016"
folder.ListItemAllFields.Update()
context.ExecuteQuery()
End Using
Result
you have to set the properties on the Item property of the Document Set object. Like this (sorry for c# code):
DocumentSet myDocSet = DocumentSet.Create(x, x, x, x):
SPListItem myDocSetItem = myDocSet.Item;
myDocSetItem[property] = value;

RDLC local report trying to render before loading datasource

I have created a local report that contains a few sub reports. I am trying to load the report straight to pdf based off the click of a button.
I have tried showing the report with report viewer and it shows up fine, but when I try to render directly to pdf I get an error about data source. When I debug the code I notice that my subprocessing function does run until after the button call function finishes and throws the error.
Dim reportParam As New ReportParameter("appId", appid)
Dim reportParam2 As New ReportParameter("appDate", appDate)
Dim reportParam3 As New ReportParameter("auditDate", Now)
Dim reportArray As New ReportParameterCollection
reportArray.Add(reportParam)
reportArray.Add(reportParam2)
reportArray.Add(reportParam3)
AddHandler ReportViewer1.LocalReport.SubreportProcessing, AddressOf SetSubDataSource
ReportViewer1.LocalReport.SetParameters(reportArray)
ObjectDataSource1.SelectParameters("appID").DefaultValue = appid
ObjectDataSource1.SelectParameters("appDate").DefaultValue = appDate
ObjectDataSource1.DataBind()
Dim warnings As Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Try
ReportViewer1.DataBind()
ReportViewer1.LocalReport.Refresh()
Dim byteViewer As Byte()
byteViewer = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
Response.Buffer = True
'Response.Clear()
Response.ContentType = mimeType
Response.AddHeader("content-disposition", "attachment; filename=test.pdf")
Response.BinaryWrite(byteViewer)
Response.OutputStream.Write(byteViewer, 0, byteViewer.Length)
Response.Flush()
Response.Close()
Thanks for your advice in advance, I just trying to figure out how to get the report to load data sources before it renders and not after this function is complete.

VB.net loading out of date webpages

I use the following code to copy a certain line of text from the website listed.
'Track A
Dim TrackA As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://pilotweb.nas.faa.gov/common/nat.html")
Dim GaA As System.Net.HttpWebResponse = TrackA.GetResponse
Dim WrA As System.IO.StreamReader = New System.IO.StreamReader(GaA.GetResponseStream)
Dim ContentStrA As String = WrA.ReadToEnd
Dim StartIndexA As Integer = ContentStrA.IndexOf(vbLf & "A ") + 1
Dim StrLengthA As Integer = ContentStrA.IndexOf(vbLf, StartIndexA) - StartIndexA
However when I open the link in a tab in VB, it shows a version of the site from a few days ago, how do I prevent this from happening as I need the current version of the site?
Change the Cache Policy of your System.Net.HttpWebRequest object, so that you're not getting cached results.
Alternatively, you can clear the cache for your URL.