Search This Blog

Monday, May 30, 2016

How to filter the content type which has Information Management Policy Settings in SharePoint online

All the Information management policy settings information can be obtained from the SchemaXML of the content type.

Below XML is an extract from the schema XML which contains details about the policies. Here I do have a retention policy attached to my content type.

<XmlDocument NamespaceURI="office.server.policy">
- <p:Policy xmlns:p="office.server.policy" id="" local="true">
  <p:Name>DemoContentType</p:Name>
  <p:Description />
  <p:Statement />
- <p:PolicyItems>
- <p:PolicyItem featureId="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration" staticId="0x0101007F2D345A54104E038DF20E887A9D9E57|-225302813" UniqueId="a4a001ae-0512-40f4-b6dc-36a3861a6726">
  <p:Name>Retention</p:Name>
  <p:Description>Automatic scheduling of content for processing, and performing a retention action on content that has reached its due date.</p:Description>
- <p:CustomData>
- <Schedules nextStageId="2">
- <Schedule type="Default">
- <stages>
- <data stageId="1">
- <formula id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn">
  <number>60</number>
  <property>DemoContentType</property>
  <propertyId>c8f6e1c4-183f-4bf6-8756-8428c39aab33</propertyId>
  <period>days</period>
  </formula>
  <action type="action" id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin" />
  </data>
  </stages>
  </Schedule>
  </Schedules>
  </p:CustomData>
  </p:PolicyItem>
  </p:PolicyItems>
  </p:Policy>
  </XmlDocument>

Simple code snippet to traverse all the content types and get the content types which has a policy attached to it in SharePoint online using client object model.

string url = "siteURL";
            ClientContext ctx = new ClientContext(url);
            ctx.Credentials = new SharePointOnlineCredentials("email id", new NetworkCredential("email id", "XXXX").SecurePassword);

            ContentTypeCollection contentTypeCollection = ctx.Web.ContentTypes;

            ctx.Load(contentTypeCollection, ct => ct.Include(val => val.Name, val => val.Hidden, val => val.Id, val => val.SchemaXml));
            ctx.ExecuteQuery();

            foreach (ContentType contentType in contentTypeCollection)
            {
                if (!contentType.Hidden)
                {
                    XDocument xdoc = XDocument.Parse(contentType.SchemaXml);
                    IEnumerable<XElement> elments = xdoc.Descendants().Where(elm => elm.Name.NamespaceName == "office.server.policy");

                    if (elments.Count() > 0)
                    {
                        Console.WriteLine("\"" + contentType.Name + "\"," + contentType.Id);
                    }
                }
            }

Friday, October 16, 2015

Add user to Yammer group and approve the added user in private group

Pre-requisite

1. Service account with Verified admin credentials
2. Create a app and follow the steps in this link - https://developer.yammer.com/docs/test-token
3. Client id and token (oAuth token) of verified admin is mandatory.
4. Service account has to be group admin to approve the members if only those approved by an admin is selected.

Please find the link to download the working code. client id, oAuth token of verified admin of your network needs to be updated along with the user email id and the group id to which the user has to be added.

Step 1: https://www.yammer.com/api/v1/users/by_email.json?email={0} - returns user id from the user id. If you have user id handy you can ignore this step.

Step 2: https://www.yammer.com/api/v1/oauth/tokens.json?consumer_key={0}&user_id={1} - Impersonate the user and get the oAuth token after impersonation. Below diagram explains the request flow.

Note: Above diagram is taken from the book - Develop on Yammer: Social Integration for Modern Business Applications -  By Pathik Rawal, Pryank Rohilla 

Step 3: https://www.yammer.com/api/v1/group_memberships.json?group_id={0} - Make a post call to add the user to the group with the impersonated token as Authentication - Bearer token.

Step 4: https://www.yammer.com/api/v1/group_memberships.json?group_id={0}&user_id={1}&approve=1 - Make a post call with the group admin - oAuth token to approve the request.

Step 4 is not documented in Yammer API but it works. At any point of time, it can go unsupported.

This link contains end to end project - Completed demo



Thursday, March 6, 2014

Increase KQL query length limit in SharePoint 2013

The length limit of a KQL query varies depending on how you create it. If you create the KQL query by using the default SharePoint search front end, the length limit is 2,048 characters. However, KQL queries you create programmatically by using the Query object model have a default length limit of 4,096 characters. You can increase this limit up to 20,480 characters by using the MaxKeywordQueryTextLength property or the DiscoveryMaxKeywordQueryTextLength property (for eDiscovery).

Source - MSDN - Link 

Using PowerShell the length can increased.

Code snippet:

$SearchApplicationName = "Search Service Application";
$ServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application";

$ServiceApplication.MaxKeywordQueryTextLength = 5000;

This will increase the KQL length to 5000. 

Friday, May 25, 2012

Circular linkedlist in C#

 class CustomCircularLinkedList<T>
    {
        public CircularLinkedListItem<T> RootNode;
        public int Count { set; get; }

        public void Add(T value)
        {
            CircularLinkedListItem<T> newNode = new CircularLinkedListItem<T>(value);

            if (RootNode == null)
            {
                this.RootNode = newNode;
                Count++;
                return;
            }

            CircularLinkedListItem<T> node = this.RootNode;

            while (node.NextNode != null)
            {
                node = node.NextNode;
            }

            node.NextNode = newNode;
            newNode.PreviousNode = node;

//This distinguishes double and circular linkedlist
            this.RootNode.PreviousNode = newNode;

        }
    }

    class CircularLinkedListItem<T>
    {
        public CircularLinkedListItem<T> PreviousNode { set; get; }
        public CircularLinkedListItem<T> NextNode { set; get; }
        public T NodeItem { set; get; }

        public CircularLinkedListItem(T value)
        {
            this.NodeItem = value;
        }
    }

Double Linkedlist in C#

class CustomDoubleLinkedList<T>
    {
        private CustomDoubleLinkedListItem<T> RootNode;

        public int Count { set; get; }

        public void AddValue(T value)
        {
            CustomDoubleLinkedListItem<T> CustomDoubleLinkedListItemReference = new CustomDoubleLinkedListItem<T>(value);

            if (RootNode == null)
            {               
                RootNode = CustomDoubleLinkedListItemReference;
                RootNode.PreviousNode = null;
                Count++;
                return;
            }

            CustomDoubleLinkedListItem<T> nextNodeReference= this.RootNode;

            while (nextNodeReference.NextNode != null)
            {               
                nextNodeReference = nextNodeReference.NextNode;
            }
           
            nextNodeReference.NextNode = CustomDoubleLinkedListItemReference;
            CustomDoubleLinkedListItemReference.PreviousNode = nextNodeReference;
        }

        public void InsertAt(T value, uint position)
        {

            if (position > Count)
            {
                throw new InvalidOperationException("Position cannot be greater than link nodes");
            }

            int tempCount = 0;
            CustomDoubleLinkedListItem<T> CustomDoubleLinkedListItemReference = new CustomDoubleLinkedListItem<T>(value);
            CustomDoubleLinkedListItem<T> node = this.RootNode;

            while (tempCount != position && node.NextNode != null)
            {
                node = node.NextNode;
            }

            if (node == this.RootNode)
            {
                CustomDoubleLinkedListItemReference.PreviousNode = null;
            }
            else
            {
                CustomDoubleLinkedListItemReference.PreviousNode = node.PreviousNode;
            }

            CustomDoubleLinkedListItemReference.NextNode = node;
        }

        public void ReverseList()
        {
            CustomDoubleLinkedListItem<T> node = this.RootNode.NextNode;
            CustomDoubleLinkedListItem<T> previousNode;
            CustomDoubleLinkedListItem<T> nextNode;
            while (node != null)
            {
                nextNode = node.PreviousNode;
                previousNode = node.NextNode;
                node.PreviousNode = previousNode;
                node.NextNode = nextNode;

                //Handle the old root
                if (previousNode == null)
                {
                    previousNode = this.RootNode.NextNode;
                    this.RootNode.NextNode = null;
                    //Set the previous node
                    this.RootNode.PreviousNode = previousNode;
                    this.RootNode = node; 
                    previousNode = null;
                }

                if (previousNode == this.RootNode)
                {
                    node.PreviousNode = null;
                }

                node = previousNode;
            }           
        }


        public string DrawNode()
        {
            string output = String.Empty;

            if (RootNode.NextNode == null)
            {
                return RootNode.Value.ToString();
            }

            output = RootNode.Value.ToString();
            CustomDoubleLinkedListItem<T> nextNodeReference = this.RootNode.NextNode;

            while (nextNodeReference.NextNode != null)
            {
                output = String.Concat(output,",", nextNodeReference.PreviousNode.Value.ToString(), "->", nextNodeReference.Value);
                nextNodeReference = nextNodeReference.NextNode;
            }

            return output;
        }
    }

    class CustomDoubleLinkedListItem<T>
    {
        public CustomDoubleLinkedListItem<T> PreviousNode { set; get; }
        public CustomDoubleLinkedListItem<T> NextNode { set; get; }
        public T Value { set; get; }

        public CustomDoubleLinkedListItem(T customDoubleLinkedListItemValue)
        {
            this.Value = customDoubleLinkedListItemValue;
        }
    }

LinkedList

    class CustomSingleLinkedList<T>
    {
        private CustomLinkedListItem<T> rootNode;
        public int Count = 0;

        public void Add(T value)
        {
            CustomLinkedListItem<T> linkedListItem = new CustomLinkedListItem<T>(value);

            if (rootNode == null)
            {
                rootNode = linkedListItem;
                ++Count;
                return;
            }

            CustomLinkedListItem<T> previousNode = rootNode;

            while (previousNode.Reference != null)
            {
                previousNode = previousNode.Reference;               
            }

            previousNode.Reference = linkedListItem;
            ++Count;
        }


        public void InsertAt(T value, int position)
        {
            if (position > Count)
            {
                throw new IndexOutOfRangeException();
            }

            int tempCount = 0;
            CustomLinkedListItem<T> linkedListItem = new CustomLinkedListItem<T>(value);
            CustomLinkedListItem<T> node = rootNode;
            CustomLinkedListItem<T> tempnode = null;

            while (tempCount != position && node.Reference != null)
            {
                tempnode = node;
                node = node.Reference;
                tempCount++;
            }

            if (tempnode != null)
            {
                tempnode.Reference = linkedListItem;
            }
            else
            {
                this.rootNode = linkedListItem;
            }
           
            linkedListItem.Reference = node;          
        }

        public void ChangeValueAt(int position, T value)
        {
            int tempCount = 1;
            CustomLinkedListItem<T> node = rootNode;

            while (tempCount != position && node.Reference != null)
            {
                node = node.Reference;
                tempCount++;
            }

            node.Item = value;
        }

        public T ValueAt(int position)
        {
            T value = default(T);

            int tempCount = 1;
            CustomLinkedListItem<T> node = rootNode;

            while (tempCount != position && node.Reference != null)
            {
                node = node.Reference;
                tempCount++;
            }

            value = node.Item;
            return value;
        }

        public void ReverseLinkedList()
        {
            int tempCount = Count;

            for (int i = 1, j = tempCount; i < j; i++, j--)
            {
                T Temp = ValueAt(i);
                ChangeValueAt(i, ValueAt(j));
                ChangeValueAt(j, Temp);
            }
        }

        public T ReturnLastItem()
        {
            if (rootNode == null)
            {
                throw new InvalidOperationException("Root node is null");
            }

            if (rootNode.Reference == null)
            {
                return rootNode.Item;
            }

            CustomLinkedListItem<T> node = rootNode;

            while (node.Reference != null)
            {
                node = node.Reference;
            }

            return node.Item;
        }

        public void RemoveLastItem()
        {
            if (rootNode == null)
            {
                throw new InvalidOperationException("Root node is null");
            }

            if (rootNode.Reference == null)
            {
                rootNode = null;
                return;
            }

            CustomLinkedListItem<T> node = this.rootNode;
            CustomLinkedListItem<T> tempnode = null;

            while (node.Reference != null)
            {
                tempnode = node;
                node = node.Reference;
            }

            tempnode.Reference = null;
        }

        public void RemoveFirstItem()
        {
            if (rootNode == null)
            {
                throw new InvalidOperationException("Root node is null");
            }

            if (rootNode.Reference != null)
            {
                rootNode = rootNode.Reference;
            }
            else
            {
                rootNode = null;
            }
        }       
    }

    class CustomLinkedListItem<T>
    {
        public T Item { set; get; }
        public CustomLinkedListItem<T> Reference { set; get; }

        public CustomLinkedListItem(T item)
        {
            this.Item = item;
        }
    }

Thursday, May 24, 2012

Implement stack from two que

class StackFromQue
    {
        private Queue<int> a = new Queue<int>();
        private Queue<int> b = new Queue<int>();

        //Optimize for Push operation
        public void Push(int value)
        {
            a.Enqueue(value);
        }

        public int Pop()
        {
            if (a.Count.Equals(0) && b.Count.Equals(0))
            {
                throw new InvalidOperationException("Empty stack");
            }

            if (a.Count > 0)
            {
                return PopOperation(a, b);
            }
            else
            {
                return PopOperation(b, a);
            }
        }

        private int PopOperation(Queue<int> first, Queue<int> second)
        {
            while (first.Count > 1)
            {
                second.Enqueue(first.Dequeue());
            }

            return first.Dequeue();
        }
    }

Explanation:

1. For Push operation, Enque all the values in Que 1.

2. For Pop opeartion, deque all the values from the current que into the next que until there is only one value in the current que. Return the last value and deque them. For the next value swap the ques.

Note: This is optimized for Push operation. It can be optimized for Pop operation in the reverse manner.