XML Site Map

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="generateXML.aspx.vb" Inherits="Examples_generateXML" %>

<%@ Register Assembly="Ektron.Cms.Controls" Namespace="Ektron.Cms.Controls" TagPrefix="CMS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Generate Sitemap Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button runat="server" ID="btnGenerate" Text="Click to Generate Sitemap" OnClientClick="displayWait()" />
		<br />
		<br />
		<cms:login runat="server" id="login1" />
	</div>
	<div id="results" runat="server">
	</div>
	</form>
</body>
</html>
Imports System.Xml
Imports System.Text
Imports Ektron.Cms.WebSearch.SearchData
Imports Ektron.Cms.API.Search
Imports Ektron.Cms.CommonApi
Imports System.Data.SqlClient

Partial Class Examples_generateXML
Inherits System.Web.UI.Page

Public Shared connString As String = ConfigurationManager.ConnectionStrings("Ektron.DbConnection").ToString()
Public Shared sUserID As String = ""

#Region "Get Asset data from SQL - Start"
#Region "Get full file name data from SQL - Start"
Public Function sGetFileName(ByVal contentid As String) As String
	Dim sFileName As String = Nothing
	sFileName = ""
	Using conn1 As New SqlConnection(connString)
		Using cmd1 As New SqlCommand("SELECT handle FROM content c INNER JOIN AssetDataTable adt ON adt.id=c.asset_id WHERE content_id=" & contentid & ";", conn1)
			conn1.Open()
			Dim rdr1 As SqlDataReader = cmd1.ExecuteReader()
			If rdr1.HasRows Then
				While rdr1.Read()
					sFileName = rdr1("handle").ToString()
				End While
			End If
		End Using
	End Using
	Return sFileName
End Function
#End Region

#Region "Get file extension data from SQL - Start"
Public Function sGetFileExtension(ByVal contentid As String) As String
	Dim sFileExtension As String = Nothing
	sFileExtension = ""
	Using conn1 As New SqlConnection(connString)
		Using cmd1 As New SqlCommand("SELECT(CASE WHEN handle LIKE'%.%'THEN reverse(left(reverse(handle),charindex('.',reverse(handle))-1))ELSE''END)AS FilePath FROM content c INNER JOIN AssetDataTable adt ON adt.id=c.asset_id WHERE content_id=" & contentid & ";", conn1)
			conn1.Open()
			Dim rdr1 As SqlDataReader = cmd1.ExecuteReader()
			If rdr1.HasRows Then
				While rdr1.Read()
					sFileExtension = rdr1("FilePath").ToString()
				End While
			End If
		End Using
	End Using
	Return sFileExtension
End Function
#End Region
#End Region

#Region "Require visitor to login - Start"
Public Shared Function LoggedIn() As String
	Dim sLoggedIn As String = "no"
	Dim contentApi As New Ektron.Cms.ContentAPI()
	If contentApi.IsLoggedIn AndAlso contentApi.LoadPermissions(0, "users", Ektron.Cms.ContentAPI.PermissionResultType.All).IsLoggedIn Then
		sLoggedIn = "yes"
		'Allow access
		sUserID = contentApi.UserId.ToString()
	End If
	Return sLoggedIn
End Function
#End Region

#Region "Check User Group - Start"
Public Shared Function GetUserGroup(ByVal userID As Integer, ByVal GroupID As String) As Integer
	Dim inGroup As Integer = 0
	If userID <> 0 Then
		Dim query1 As String = "SELECT user_to_group_tbl.usergroup_id " & ",usergroups.usergroup_name " & ",user_to_group_tbl.user_id " & "FROM user_to_group_tbl " & "INNER JOIN usergroups ON user_to_group_tbl.usergroup_id = usergroups.usergroup_id " & "WHERE user_to_group_tbl.user_id = '" & userID & "'"

		Dim conn1 As New SqlConnection(connString)
		Dim cmd1 As New SqlCommand(query1, conn1)
		conn1.Open()
		Dim rdr1 As SqlDataReader = cmd1.ExecuteReader()
		If rdr1.HasRows Then
			While rdr1.Read()
				If rdr1("usergroup_id").ToString() = GroupID Then
					inGroup = 1
				End If

				If rdr1("usergroup_id").ToString() = "1" Then
					inGroup = 1
				End If
			End While
		End If
	End If
	Return inGroup
End Function
#End Region

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
	If LoggedIn() = "yes" AndAlso GetUserGroup(Integer.Parse(sUserID), "1") = 1 Then
		btnGenerate.Visible = True
	End If
End Sub

Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		'Uses format provided here:
		'Framework API
		Dim api As New Ektron.Cms.Framework.Core.Content.Content
		Dim criteria As New Ektron.Cms.Common.Criteria(Of Ektron.Cms.Common.ContentProperty)
		Dim cf As New Ektron.Cms.Common.CriteriaFilter(Of Ektron.Cms.Common.ContentProperty)
		Dim cdatas As New System.Collections.Generic.List(Of Ektron.Cms.ContentData)
		Dim cdata As New Ektron.Cms.ContentData
		Dim pageInfo As New Ektron.Cms.PagingInfo

		'Set criteria filter to grab all content
		cf.Field = Ektron.Cms.Common.ContentProperty.Id
		cf.Operator = Ektron.Cms.Common.CriteriaFilterOperator.GreaterThan
		cf.Value = 0

		'Set limit for API
		'default 100k
		pageInfo.RecordsPerPage = 100000
		criteria.PagingInfo = pageInfo

		'Get Content for Sitemap
		criteria.Filters.Add(cf)
		cdatas = api.GetList(criteria)

		'create XML document
		Dim writer As New XmlTextWriter(Request.PhysicalApplicationPath.ToString + "sitemapsitemap-" + DateTime.Now.ToString("yyyy-MM-dd_HHmm") + ".xml", Encoding.UTF8)
		writer.WriteStartDocument()
		writer.WriteStartElement("urlset", "xmlns=http://www.sitemaps.org/schemas/sitemap/0.9")

		Dim tempDate As New Date

		'loop through each item, write to XML
		For Each cdata In cdatas

			writer.WriteStartElement("url")
			writer.WriteElementString("name", cdata.Title)
			writer.WriteElementString("loc", "http://" + Request.Url.Host.ToString + cdata.Quicklink.ToString)
			writer.WriteElementString("folder", cdata.Path)
			writer.WriteElementString("editor", cdata.EditorFirstName + " " + cdata.EditorLastName)
			writer.WriteElementString("contenttype", cdata.ContType)
			writer.WriteElementString("filename", sGetFileName(cdata.Id))
			writer.WriteElementString("fileextension", sGetFileExtension(cdata.Id))
			writer.WriteElementString("created", cdata.DateCreated)
			writer.WriteElementString("lastmod", cdata.DateModified)


			'Content modified in the last 7 days has a higher priority
			'can be configured to modify priority
			'If cdata.DateModified > Date.Now.AddDays(-7) Then

			'	writer.WriteElementString("priority", "1.0")

			'Else

			'	writer.WriteElementString("priority", "0.5")

			'End If

			writer.WriteEndElement()

		Next

		'Stop writing XML
		writer.WriteEndElement()
		writer.WriteEndDocument()

		writer.Close()

		'write out total items added to map
		results.InnerHtml = "Sitemap Created: " + cdatas.Count.ToString + " Items Added"
	End Sub
End Class

Based Off of Ektron Code Library – http://developer.ektron.com/Templates/CodeLibraryDetail.aspx?id=4294967329&blogid=116

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.