Archive for the ‘Code’ Category


Most often you don’t come across this requirement of updating the content approval status column. Below is the scenario I had.

I had a list containing video items.  This has the content approval functionality enabled. On approval this column gets updated to approved. Approved items are displayed in search results. Opening an item displays a media player which plays the video. At this point, I update a column named “Views” in the list, which holds the view count of that video using CSOM. Unfortunately once I update this column, my content approval status became Pending. This makes my search not to crawl the item thereafter.

When i try to update this column to “Approved” in my CSOM code using the content approval column name, SharePoint was  not able to find the column name. So I found 2 things out here..

  • The content approval status column can be accesses using its internal name “_ModerationStatus“.
  • The value which we can assign to it is not Text but Integer. Because the value is defined as an enum internally which is “SPModerationStatusType“.

Below is the code detail and the possible status values & their corresponding enum values.

this.oListItemUpdate = oList.getItemById(listID);

    var wrkFlwApproval = this.oListItem.get_item(‘_ModerationStatus’);
    if(wrkFlwApproval == 0)
    {
        this.oListItemUpdate.set_item(‘_ModerationStatus’, 0 );    
    }

public enum SPModerationStatusType
{
    Approved, //0
    Denied,   //1
    Pending,  //2
    Draft,    //3
    Scheduled //4
 }

Word of caution : The user updating this column should have approver rights on the list.


When you try creating a SharePoint 2013 App project in a SharePoint hosted model and when you try to deploy the solution, you get an error “Error occurred in deployment step ‘Install app for SharePoint’: Sideloading of apps is not enabled on this site.”

One thing you should remember is, Always the app should be published/deployed to the developer site. That is, while creating a site collection/site where you want to deploy the app, create it using the “Developer Site” template under the Collaboration tab. Now you will be able to deploy your app on the site.

Convert XMLElement to DataTable

Posted: November 9, 2011 in Code

Below is the code to convert an XMLElement object containing values to a DataTable. The XMLElement tag names will be the column headings in the DataTable.

public DataTable ConvertXmlElementToDataTable(XmlElement xmlElement, string tagName)
{
XmlNodeList xmlNodeList = xmlElement.GetElementsByTagName(tagName);

DataTable dt = new DataTable();
int TempColumn = 0;
foreach (XmlNode node in xmlNodeList.Item(0).ChildNodes)
{
TempColumn++;
DataColumn dc = new DataColumn(node.Name, System.Type.GetType(“System.String”));
if (dt.Columns.Contains(node.Name))
{
dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
}
else
{
dt.Columns.Add(dc);
}
}
int ColumnsCount = dt.Columns.Count;
for (int i = 0; i < xmlNodeList.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < ColumnsCount; j++)
{
if (xmlNodeList.Item(i).ChildNodes[j] != null)
dr[j] = xmlNodeList.Item(i).ChildNodes[j].InnerText;
else
dr[j] = “”;
}
dt.Rows.Add(dr);
}
return dt;
}

The parameters for this method are the actual XMLElement & the outer tag name in the XML. For example considering the below XML,

<EmployeeDetails>
<Employee>
<ID>1</ID>
<Name>xxx</Name>
</Employee>
<Employee>
<ID>2</ID>
<Name>yyy</Name>
</Employee>
</EmployeeDetails>
 

You will be passing “Employee” as the second parameter for the method. Returned data table will be having columns “ID” & “Name”.


This post explains how you can fins the list of users who are having permissions to a particular list or a list item. Also in case if a group is directly assigned permissions we can loop through the group to find each users of that group too.

We can access the permission details of a List  or a ListItem  using the properties

  • List.RoleAssignments
  • ListItem.RoleAssignments

Below code explains how to get SPUser details form the RoleAssignment property.

    private void AddListRoleAssignmentNodes(SPList objList)
    {   
      if (objList.HasUniqueRoleAssignments)
      {
         SPRoleAssignmentCollection oRoleAssignments =
            objList.RoleAssignments;

         foreach (SPRoleAssignment oRoleAssignment in oRoleAssignments)
         {
            SPPrincipal oPrincipal = oRoleAssignment.Member;
            try
            {
               // Retrieve users having explicit permissions on the list
               SPUser oRoleUser = (SPUser)oPrincipal;
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
            try
            {
               // Retrieve user groups having permissions on the list
               SPGroup oRoleGroup = (SPGroup)oPrincipal;

               if (oRoleGroup.Users.Count > 0)
               { 
                  string strGroupName = oRoleGroup.Name;
                  // Add code here to retrieve Users inside this User-Group
               }
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
         }
      }
   }

The first try catch gets all the users and second one gets all the groups that are having permissions on the list/list item.


  • Scrolling announcement web part which on click will take you to the given link.
  • Dynamic values and not static content.
  • Values are coming from the list with two different columns Title(Text to show) & Link (Link to redirect).
  • Uses JavaScript to scroll the content.

You can get the code here