Total Pageviews

Monday, August 24, 2020

 Updating a hierarchy path


  static void GeneratePath(List<HierarchyNode> all, HierarchyNode current)

        {

            string path = "";

            Action<List<HierarchyNode>, HierarchyNode> GetPath = null;


            GetPath = (List<HierarchyNode> ps, HierarchyNode p) => {

                var parents = all.Where(x => x.HierarchyNodeId == p.ParentNodeId);

                foreach (var parent in parents)

                {

                    path += $"/{ parent.Name}";

                    GetPath(ps, parent);

                }

            };


            GetPath(all, current);

            string[] split = path.Split(new char[] { '/' });

            Array.Reverse(split);


            current.Path = string.Join(" > ", split) + current.Name;

        }

Tuesday, July 14, 2020

Flatten tree view data

//Nested tree list, result list contains list of lists
 List<NavigationItem> allitems= result;

//Flatten the list
 List<NavigationItem> childitems = Flatten(allitems, a=>a.Items).ToList();


   public  List<NavigationItem> Flatten(List<NavigationItem> rootLevel, Func<NavigationItem, List<NavigationItem>> nextLevel)
        {
            List<NavigationItem> accumulation = new List<NavigationItem>();
            int i = 0;
            flattenLevel(accumulation, rootLevel, nextLevel, i);
            return accumulation;
        }

     
        private  void flattenLevel(List<NavigationItem> accumulation, List<NavigationItem> currentLevel, Func<NavigationItem, List<NavigationItem>> nextLevel, int level)
        {
            level++;

            foreach (NavigationItem item in currentLevel)
            {
             
                item.LevelNo = level;
                item.Level = "Level " + level.ToString();
                accumulation.Add(item);
                flattenLevel(accumulation, nextLevel(item), nextLevel, level);
            }
        }

Wednesday, July 1, 2020

Loading Treeview fast

Func<Employee, int> getXId = (x => x.EmployeeId);
Func<Employee, int?> getXParentId = (x => x.ManagerId);
Func<Employee, string> getXDisplayName = (x => x.Name);
LoadListItems(employees, getXId, getXParentId, getXDisplayName);

 private void LoadListItems<T>(IEnumerable<T> items, Func<T, int> getId, Func<T, int?> getParentId, Func<T, string> getDisplayName)
        {
            dicNodeList.Clear();

            var result = new List<NavigationItem>();

            foreach (var item in items)
            {
                var id = getId(item);
                var displayName = getDisplayName(item);
                var node = new NavigationItem { ID = id, Name = displayName, NodeInfo = item };
                node.Items = new List<NavigationItem>();
                dicNodeList.Add(getId(item), node);

            }

            foreach (var id in dicNodeList.Keys)
            {
                var currentNode = GetXNode(id);
                var nodeInfo = (T)currentNode.NodeInfo;
                var parentNodeID = getParentId(nodeInfo);

                if (parentNodeID.HasValue && dicNodeList.ContainsKey(parentNodeID.Value))
                {
                    var parent= GetXNode(parentNodeID.Value);
                    parent.Items.Add(currentNode);

                }
                else
                {
                    result.Add(currentNode);
                }
            }


            var z = result;

        }

public NavigationItem GetXNode(int id)
{
            return dicNodeList[id];
}

 Dictionary<int, NavigationItem> dicNodeList = new Dictionary<int, NavigationItem>();


 public class NavigationItem
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int? ParentID { get; set; }
        public List<NavigationItem> Items { get; set; }
        public object NodeInfo { get; set; }

    }


Sunday, April 2, 2017

Updating site http to https

Add HTTP response header as below on IIS

Content-Security-Policy : upgrade-insecure-requests

Above will treat http request as secured.

Add a rule as below between <rules></rules> section

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

Monday, March 13, 2017

Getting IIS hosted web site's information

VBScript will fetch IIS web site's information. Just download and double click to run, a text file will be created with necessary information.

https://drive.google.com/open?id=0B8R1wPP0Gj58SDFTUjlhdWk2Um8



Monday, February 13, 2017

.Getting .NET framework versions

Go to CMD

This will display framework version

cmd:\> reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\full" /v version

Monday, October 19, 2015

Uploading fles to specific folder on BOX

Refresh token is over written on each request and store on text file.


 Function UploadToBox(ByVal attachedFilename As String, ByVal stream As System.IO.Stream) As Boolean

        Dim clientID As String
        Dim clientSecret As String
        Dim oldRefreshToken As String
        Dim newToken As BoxApi.V2.Authentication.OAuth2.OAuthToken

        clientID = ConfigurationManager.AppSettings("clientid")
        clientSecret = ConfigurationManager.AppSettings("clientsecret")

        Dim tokenProvider As New TokenProvider(clientID, clientSecret)

        '''' Reading Refresh token from the file
        Dim streamReader As StreamReader
        streamReader = System.IO.File.OpenText(Server.MapPath("~\\Box\\BoxApiRefreshToken.txt"))
        oldRefreshToken = streamReader.ReadToEnd()
        streamReader.Close()

        newToken = tokenProvider.RefreshAccessToken(oldRefreshToken)
        Dim boxManager As New BoxManager(newToken.AccessToken)

        '''' Writing the new Refresh token to the file
        Dim streamWriter As New StreamWriter(Server.MapPath("~\\Box\\BoxApiRefreshToken.txt"))
        streamWriter.Write(newToken.RefreshToken)
        streamWriter.Close()

        Dim uploadFolder As Folder

        ''''rootFolder = boxManager.GetFolder(Folder.Root)
        uploadFolder = boxManager.GetFolder(ConfigurationManager.AppSettings("uploadFolderID"))

        Dim fileName As String
        fileName = System.IO.Path.GetFileNameWithoutExtension(attachedFilename)
        fileName = fileName + "-" + Guid.NewGuid.ToString + System.IO.Path.GetExtension(attachedFilename)

        boxManager.CreateFile(uploadFolder, fileName, ConvertStreamToByteArray(stream))


    End Function
Convert stream to byte array
    Private Function ConvertStreamToByteArray(ByVal stream As System.IO.Stream) As Byte()

        Dim streamLength As Long = Convert.ToInt64(stream.Length)
        Dim fileData As Byte() = New Byte(streamLength) {}
        stream.Position = 0
        stream.Read(fileData, 0, streamLength)
        stream.Close()

        Return fileData

    End Function