Thursday, September 25, 2014

Power Shell Script to grant a group permission recursively in a SharePoint Library


My task today was to write a script to recursively grant this particular group "full permission" in this document library.Obviously this is a tedious task especially when you have lots of items.
So Power shell to the rescue. I hope someone out there can use it.




##
#Make Sure snapin is loaded
##

$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
    Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Here we want to give Full Control permission to SharePoint_SP_Team on all items this library recursively
$web = get-SPWeb("http://yourServerhere")
$list = $web.Lists
$PermissionLevel = "Full Control"
$GroupName = "SharePoint_SP_TEAM"
if ($list -ne $null)
{
    #Go through each Item and Change Permission
    foreach($RootItem in $list)
    {
        if($RootItem.Hidden -eq $False)
        { 
            if ($RootItem.HasUniqueRoleAssignments -eq $False)
            {
                $RootItem.BreakRoleInheritance($True)
            }

            if ($RootItem.HasUniqueRoleAssignments -eq $True)
            {
                ForEach ($SiteGroup in $web.SiteGroups)
                {                   

                    if ($SiteGroup.Name -match $GroupName)
                    {
                        write-host $SiteGroup.Name
                        $GroupName = $SiteGroup.Name
                        $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($SiteGroup)
                        $roleDefinition = $web.RoleDefinitions[$PermissionLevel];
                        $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
                        $RootItem.RoleAssignments.Add($roleAssignment)
                        $RootItem.Update();
                        write-host $RootItem.Title
                        Write-Host "Successfully added <$GroupName>" -foregroundcolor Green
                    }               
              
             }
            }
         }
    }
}


Wednesday, September 24, 2014

When Environment.Newline and “\r\n” fails to create a new line.



I had a unique situation where I needed to display some document links and I tried using both Environment.Newline and “\r\n” and none of these gave me the result I needed. Here is the situation below.

Document 1 = “Hello1.docx”
Document2 = “Hello2.pptx”
Document3 = “Hello3.xls”

All three documents reside in a document library with the following links

Document1 = https://YourServerName/SharedLibrary/ Hello1.docx
Document1 = https://YourServerName/SharedLibrary/ Hello2.pptx
Document1 = https://YourServerName/SharedLibrary/ Hello3.xls

Here is my html code to display them.
outHtml1 = @”<a class='static menu-item'target='_blank' href='https://YourServerName/SharedLibrary/Hello.docx'><span class='additional-background'><span class='menu-item-text'>Hello1 Doc</span></span></a><br />” + “\r\n”;

outHtml2 = @”<a class='static menu-item'target='_blank' href='https://YourServerName/SharedLibrary/Hello2.pptx'><span class='additional-background'><span class='menu-item-text'>Hello2 pptxc</span></span></a><br />” + + “\r\n”;

outHtml3 = @”<<a class='static menu-item'target='_blank' href='https://YourServerName/SharedLibrary/Hello3.xls'><span class='additional-background'><span class='menu-item-text'>Hello3 xls</span></span></a><br />” + “\r\n”;




        /// <summary>
        /// Get Aritifacts by GroupID
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        ///
        private bool GetArtifactsByGroup(SPList list, string ValidationProcedure, string Classification, string Group, out string strNavigationURL, string webAbsoluteUrl, out string strGroups)
        {
            bool blnFound = false;
            string outNavUrl = string.Empty;
            string strGroupList = string.Empty;
            string outHtml = string.Empty;
            StringBuilder sb = new StringBuilder();
            try
            {

                SPSecurity.RunWithElevatedPrivileges(
                  delegate()
                  {

                      SPListItemCollection myList = null;
                      SPQuery qryGetArtifactsByGroup = new SPQuery();
                      qryGetArtifactsByGroup.ViewAttributes = "Scope=\"FilesOnly\"";
                      qryGetArtifactsByGroup.Query = string.Concat("<Where>",
                       "<And>",
                           "<And>",
                              "<Eq>",
                                 "<FieldRef Name='Validation_x0020_Procedure' />",
                                 "<Value Type='Lookup'>" + ValidationProcedure + "</Value>",
                                "</Eq>",
                              "<Eq>",
                                 "<FieldRef Name='Classification' />",
                                 "<Value Type='Choice'>" + Classification + "</Value>",
                             "</Eq>",
                           "</And>",
                           "<Eq>",
                              "<FieldRef Name='Group' />",
                              "<Value Type='Lookup'>" + Group + "</Value>",
                           "</Eq>",
                        "</And>",
                     "</Where>");

                      qryGetArtifactsByGroup.ViewAttributes = "Scope = 'Recursive'";
                      qryGetArtifactsByGroup.ViewFields = @"<FieldRef Name='Title' />
                                                                  <FieldRef Name='Name' />
                                                                  <FieldRef Name='Group' />
                                                                  <FieldRef Name='Validation_x0020_Procedure' />
                                                                  <FieldRef Name='Classification' />
                                                                  <FieldRef Name='FileRef' />
                                                                  <FieldRef Name='ContentType' />
                                                                  <FieldRef Name='DocumentID' />
                                                                  <FieldRef Name='Group_x003a_Groups' />
                                                                  <FieldRef Name='Group_x003a_ID' />
                                                                  <FieldRef Name='GroupID' />";
                      myList = list.GetItems(qryGetArtifactsByGroup);

                      if (myList != null && myList.Count > 0)
                      {
                          foreach (SPListItem item in myList)
                          {
                              strGroupList = string.Concat(TranslateLookup(item, "Group").Trim(), "\r\n");
                              outNavUrl = webAbsoluteUrl + string.Format("/{0}", SPEncode.UrlDecodeAsUrl(item.Url)).Trim();
                              outHtml += @"<a class='static menu-item' target='_blank'  href='" + outNavUrl + "'><span class='additional-background'><span class='menu-item-text'>" + item.Name + "</span></span></a>" + "\r\n";
                              sb.AppendLine("<a class='static menu-item'  target='_blank' href='" + outNavUrl + "'><span class='additional-background'><span class='menu-item-text'>" + item.Name + "</span></span></a>" + "<br />");
                              blnFound = true;
                          }

                      }
                  });

            }
            catch (Exception err)
            {
                DispayErrormessage(err);
            }
           
            strNavigationURL = sb.ToString();
            strGroups = strGroupList;
            return blnFound;
        }


From the code above, the outHtml did not provide the needed line break.
The StringBuilder, Sb.AppenLine gave me the needed linebreak. I hope this may help someone out there with the same issue.
This should have worked. But, it did not work for me. The actual result is shown below:

Thursday, September 4, 2014

SharePoint Designer Server Error: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator



SharePoint Designer Server Error: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.




If you see the error below:


 

It means you have to change your Listview threshold as follows:

Change the List View Threshold option:

  1. Login to Central Admininstration;
  2. Go to Application Management -> Manage Web Applications;
  3. Select SharePoint Central Administration WebApplication;
  4. In the ribbon above, click General Settings. That will bring down a menu, from which you should pick Resource Throttling;
  5. Change the List View Threshold option 30000 and press OK;

This solved my issue. I hope it resolves yours too.