Tuesday, December 2, 2014

Adding “Everyone” Permissions Programmatically in SharePoint 2013

Introduction

Normally, we are having permission level operations in SharePoint. We may assign such kind of permission both manually and programmatically. Here, we will see how to give permission to "Everyone" programmatically

Code


SPUser allUsers = web.AllUsers[@”c:0(.s|true”]; //c:0(.s|true is Everyone
SPRoleDefinitionCollection roleCollection = web.RoleDefinitions;
SPRoleDefinition roleDefinition = roleCollection[“Visitors”];

SPRoleAssignment roleAssignment = newSPRoleAssignment((SPPrincipal)allUsers);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

list.BreakRoleInheritance(false);
list.RoleAssignments.Add(roleAssignment);

list.Update();

Powershell script

Set-SPUser -Identity "c:o(.s\true" -web "webUrl" -Group "Visitors"

Wednesday, November 26, 2014

Delete webparts from Webpart Gallery

Introduction

We may came across the situation to remove all the custom webparts while feature deactivating. Here we are going to see how to remove the webparts programmatically.

Code

Paste the below code in FeatureDeactivating method

  SPSite site = properties.Feature.Parent as SPSite;
            if (site != null)
            {
                var definedElements = properties.Definition.GetElementDefinitions(CultureInfo.InvariantCulture);
                var allFiles = definedElements.Cast<SPElementDefinition>()
                    .SelectMany(x => x.XmlDefinition.ChildNodes.Cast<XmlElement>()
                        .Where(f => f.Name.Equals("File"))
                        .Select(f => f.Attributes["Url"].Value)).ToList();
                var webPartGallery = site.RootWeb.Lists["Webpart Gallery Name"];

                var webPartsToDelete = webPartGallery.Items.Cast<SPListItem>()
                    .Where(w => allFiles.Contains(w.File.Name)).ToList();

                for (var index = webPartsToDelete.Count - 1; index >= 0; --index)
                {
                    var webPartItem = webPartsToDelete[index];
                    webPartItem.Delete();
                }
            }

Tuesday, November 25, 2014

Delete folder from Catalog masterpage

Introduction

We may came across this situation to delete a folder in catalog master page when you deactivating feature. Here we are going to see, how can we delete this kind of folder programmatically

Code

using (SPSite site = new SPSite(siteUrl))
{
 SPList listCatalog = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
                site.AllowUnsafeUpdates = true;
                SPFolder masterpageFolder = listCatalog.RootFolder.SubFolders["FolderName"];
                if (masterpageFolder != null)
                {
                    masterpageFolder.Delete();
                }
                site.AllowUnsafeUpdates = false;
                site.RootWeb.Update();
}

Friday, November 14, 2014

Join This Community Button Not visible on Community Site


In some case we may faced this issue in community site. To enable this button select "Enable Auto-Approval" in Community Settings. 

Note: This option available when Community site is in root site. This Feature will applicable for subsites of Community root site.


Wednesday, October 15, 2014

Set Search and Offline Availability programmatically

Introduction

As all we know SharePoint is having an option to search a content through out the site. We can able to skip some sites in search crawling. Normally, we can do this manually in the site.
Goto Site Settings --> Search and Offline Availability and set option as you want.

Let see, How can we set this programmatically.

Code

var contextWeb = SPContext.Current.Web;
contextWeb.AllowUnsafeUpdates = true;
contextWeb.AllowAutomaticASPXPageIndexing = false;
contextWeb.ASPXPageIndexMode = WebASPXPageIndexMode.Never;
contextWeb.NoCrawl = true;
contextWeb.Update();
contextWeb.AllowUnsafeUpdates = false;

Monday, October 13, 2014

Applying site policy programmatically in SharePoint 2013

Introduction

We all know that about Site Closure and deletion. Click here to know some notes about it. Here, i am going to give an idea to apply this site policy to subsites programmatically. After setting Site Closure and Deletion follow the below instructions.

Site Policy

Refer  Microsoft.Office.RecordsManagement.InformationPolicy dll in your project and inherit this in the page

using Microsoft.Office.RecordsManagement.InformationPolicy;

var contextWeb = SPContext.Current.Web;
 contextWeb.AllowUnsafeUpdates = true;

ProjectPolicy groupPolicy = (from s in ProjectPolicy.GetProjectPolicies(contextWeb.Site.OpenWeb()) where s.Name == "Your Sit Policy Name" select s).FirstOrDefault();
                ProjectPolicy.ApplyProjectPolicy(contextWeb, groupPolicy);
contextWeb.Update();
contextWeb.AllowUnsafeUpdates = false;

That's all Site Policy will apply in your site. To ensure that, Goto Site Closure and Deletion in Site Settings and check applied Site Policy.

Tuesday, October 7, 2014

Set Property value for a Site Programmatically

Introduction

Property bag is having property with their Key and Value. Its used to keep value and used it within a site. It will be used across the subsites, If we set a property in Main site collection. Let see how to set this property programmatically.

Set Property

var web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
web.Properties.Add("Key", "Value");
web.Properties.Update();
web.AllowUnsafeUpdates = false;

//Get a Property value
var r = SPContext.Current.Web.Properties["Your Property Key"]