Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts

Tuesday, July 9, 2013

Delete Social Tags in SharePoint 2010 using C#

Hi all,
I've recently wanted to delete social tags when I am deploying my wsp using powershell. I started writing the code and was able to retrieve the social tags but was not able to delete them. When I attempted to delete them an exception was thrown. More like an Unauthorized exception. Read this for more information on it.

But I was able to find this blog post which helped me out in solving the issue.  I was able to create a console program using C# to delete the social tags of SharePoint 2010. I havent tested it much. Feel free to modify the code.
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Web;
using Microsoft.Office.Server.SocialData;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace RemoveTags
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite(args[0]))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Deleting all tags");
                    foreach (SPUser user in site.OpenWeb().AllUsers)
                    {
                        if (!user.IsDomainGroup && user.LoginName != "NT AUTHORITY\\LOCAL SERVICE" && user.LoginName != "SHAREPOINT\\system")
                        {
                            web.AllowUnsafeUpdates = true;
                            Console.WriteLine("Deleting tags for : " + user.Name);
                            HttpRequest request = new HttpRequest("", args[0], "");
                            HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));
                            HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
                            WindowsIdentity wi = WindowsIdentity.GetCurrent();
                            typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(wi, user.LoginName);
                            HttpContext.Current.User = new GenericPrincipal(wi, new string[0]);
                            WindowsIdentity wi2 = WindowsIdentity.GetCurrent();
                            SPServiceContext serviceContext = SPServiceContext.GetContext(HttpContext.Current);
                            SocialTagManager socialTagManager = new SocialTagManager(serviceContext);
                            SPServiceContext context = SPServiceContext.GetContext(site);
                            UserProfileManager usrPrfMngr = new UserProfileManager(context);
                            UserProfile usrPrf = usrPrfMngr.GetUserProfile(user.LoginName);
                            SocialTag[] tags = socialTagManager.GetTags(usrPrf);

                            foreach (SocialTag tag in tags)
                            {
                                Term term = tag.Term;
                                socialTagManager.DeleteTag(tag.Url, term);

                            }
                        }
                    }

                    web.AllowUnsafeUpdates = false;

                    Console.WriteLine("All tags are deleted");
                    Console.ResetColor();
                    Console.ReadLine();
                }
            }
        }
    }
}

Wednesday, March 27, 2013

Delete a Field(Column) in SharePoint List using PowerShell

Hello People,
The following is a code snippet from PowerShell to completely remove or delete a field in a SharePoint list. The code uses the GUID to find the field (column). I tested this with SharePoint 2010. Feel free to modify the code. :)

#Source Help - http://dotnetfollower.com/wordpress/2012/07/sharepoint-how-to-delete-a-list-fieldcolumn-programmatically/

$web=Get-SPWeb "http://mySite/"
$list=$web.Lists.TryGetList("My List Name")
if ($list -ne $null) {
    foreach($column in $list.Fields){
        if ($column.Id -eq "[GUID]") {
            write-host -f green "Deleting column with Internal name as : " $column.InternalName
            
            if ($column.ReadOnlyField)
            {
                $column.ReadOnlyField = $false;
                $column.Update()
            }

            if ($column.Hidden)
            {
                $column.Hidden = $false;
                $column.Update()
            }
     
            if ($column.AllowDeletion -eq $null -or !$column.AllowDeletion.Value)
            {
                $column.AllowDeletion = $true
                $column.Update()
            }
     
            $column.Delete()
            $column.ParentList.Update()

        }
    }
}
else
{
    write-host "list is null"
}

Tuesday, December 11, 2012

" 'Model': object is null " error in Telerik RadSchedular

Recently I was using RadSchedular on an application page for SharePoint. I came across this weird problem on the navigation of the RadSchedular. i.e "Day", "Week", "Month" etc. I can switch it once, but afterwards I cannot switch it back. I noticed an script error shown in the browser as.

" Scriptresource.axd of the property 'Model': object is null "

So I googled and found a similar problem in the Telerik forum

http://www.telerik.com/community/forums/aspnet-ajax/scheduler/asp-updatepanel-and-radscheduler-issues.aspx

The Fix :

Add this tag to the telerik control.
EnableEmbeddedScripts="false"

Then in the OnInit function of the page, do this...
protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
            scriptManager.ScriptMode = ScriptMode.Debug;
            string telerikAssembly = typeof(RadScheduler).Assembly.FullName;
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Common.Core.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Common.jQuery.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Common.jQueryPlugins.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Common.Popup.PopupScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Common.Navigation.NavigationScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Menu.RadMenuScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Menu.ContextMenu.RadContextMenuScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Input.TextBox.RadInputScript.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Input.DateInput.RadDateInputScript.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Input.NumericTextBox.RadNumericInputScript.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Calendar.RadTimeViewScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Calendar.RadCalendarCommonScript.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Calendar.RadCalendarScript.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Calendar.RadDatePicker.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Calendar.RadDateTimePickerScript.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.ComboBox.RadComboBoxScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Helpers.DateTime.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.RadSchedulerScripts.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.RecurrenceEditor.RecurrenceEditor.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.ContextMenu.RadScheduler.ContextMenu.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Week.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Week.GroupedByResource.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Week.GroupedByDate.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.MultiDay.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.MultiDay.GroupedByResource.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.MultiDay.GroupedByResource.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Day.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Day.GroupedByResource.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Month.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Month.GroupedByResource.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Month.GroupedByDate.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Timeline.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Timeline.GroupedByResource.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Views.Timeline.GroupedByDate.Model.js", telerikAssembly));
            scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.Scheduling.AdvancedTemplate.js", telerikAssembly));
        }

For some reason I had to comment this line, It gave an error on the page load.
scriptManager.Scripts.Add(new ScriptReference("Telerik.Web.UI.Scheduler.ContextMenu.RadScheduler.ContextMenu.js", telerikAssembly));

Afterwards the navigation worked like a charm.


Updates : This issue was also solved by Update Sharepoint 2010.

Tuesday, December 4, 2012

Create Site Permission Groups and Add Users for SharePoint - Powershell

The following post is a PowerShell script to create or remove site level permission groups and add users to those group for SharePoint.
Function to create a permission group...

function Create-SPGroupInWeb  
{  
    param ($url, $groupName, $permissionLevel, $description)  
    try{
        $web = Get-SPWeb -Identity $url 
        if($web -ne $null){
            if ($web.SiteGroups[$groupName] -ne $null)  
            {  
                write-Host -f red "Group $groupName already exists!"
            }  
            else  
            {  
                $web.SiteGroups.Add($groupName, $web.Site.Owner, $web.Site.Owner, $description)  
                $group = $web.SiteGroups[$groupName]  
                $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)  
                $roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]  
                $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)  
                $web.RoleAssignments.Add($roleAssignment)  
                $web.Update()  
                write-Host "Group $groupName created successfully" 
            }  

            $web.Dispose() 
        }
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}

Function to remove permission groups by name...
function Remove-SPGroupInWeb  
{  
    param ([string]$url, [string]$groupName)  
    try{
        $web = Get-SPWeb -Identity $url 
        if($web -ne $null){
            if($web.SiteGroups[$groupName] -ne $null){
                write-Host -f yellow "Removing site group $groupName"
                $web.SiteGroups.Remove($groupName)
            }
            $web.Update()
            $web.Dispose() 
        }
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}

Function to add users to the group...
function Add-User
{
    param ($url, $groupName, $userName) 
    try{
        $web = Get-SPWeb $url
        if($web -ne $null){
            $MyGrp = $web.SiteGroups[$groupName]
            $user = $web.Site.RootWeb.EnsureUser($userName)
            $MyGrp.AddUser($user)
            write-Host "$userName added to $groupName" 
            $web.Update()
            $web.Dispose()
        }
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}

How to call the functions...
Remove-SPGroupInWeb -url "http://my-webApp:port/" -groupName "New Group"

Create-SPGroupInWeb -url "http://my-webApp:port/" -groupName "New Group" -permissionLevel "Read" -description "My New Group Description"

Add-User -url "http://my-webApp:port/" -groupName "New Group"-userName "domain\myUserName" 


As for the permission level of the group your going to create, For full control, send "Full Control" as the permissionLevel parameter. Running this code directly in SharePoint Management Shell wont be a problem.
Incase you are running it on Windows PowerShell, run this command as well... "Add-PSSnapin "Microsoft.SharePoint.PowerShell"


If you have any questions. I'll be glad to reply with what ever I know :) .