Programatically breaking inheritance and adding unique permission to List Items

Permission handling is something which makes SharePoint a favorite product within the industry. SharePoint has Permission levels in built for basic roles, however you can create your own permission levels and assign to Groups and then set those groups to access any site/library/list. Whenever a sub-site is created, it asks you if you want to inherit the permission from the parent site or want to create unique permissions. 

If you choose to inherit the permissions, it will copy the same set of permissions to the sub-site which will again be passed on to the lists/libraries you create. It may happen that for securing our content, we want to break this inheritance and want to assign unique permissions to restrict the users for certain actions and to make the content more secure. Inheritance can be broken manually using the Out of feature also and if you have a business logic in place, you can do it programmatically also. This blog post gives you code to break inheritance for items meeting certain criteria and assigning permissions to them explicitly. I am using ItemAdded() event receiver to do this. You can use it within workflow or any custom webpart as well.

SPGroup securityGroup = spWeb.SiteGroups["Security Group Name"]; 
SPRoleDefinition groupRole = spWeb.RoleDefinitions["Read"];

SPRoleAssignment roleAssign = new SPRoleAssignment(securityGroup);

SPListItem listItem = spWeb.GetListItem("http://List Item URL");

SharePoint 2010 : URL Mapping issue

At times, when you are writing some code or accessing your site and want to use IP address to locate your SharePoint but you see the error below. Follow the steps mentioned to resolve your issue:

The Web application at http://your IP address could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application

  • Add the IP in the Access mapping in SharePoint Central Admin.
  • Select Alternate Access Mapping Collection as your team site.
  • Add Internal URL as your IP and select zone as Intranet.

Resizing an Image for consistency in SharePoint

While implementing intranet within our organization, we configure MySites within SharePoint, upload the user profiles from Active Directory and give users the ability to upload their own picture. Also, while implementing any News, or any company information, updates on the home page of Intranet, we tend to put pictures beside the text we write. Now, the image for several notifications can be of several size if you don’t have an in-house designer who trims/resizes your pictures to be uploaded to SharePoint. So, here comes my solution to resize images as we upload them to a definitive size which can then be uploaded and shown to users on the SharePoint.

I have created Synchronous event receiver to my Notification list where I am adding the image and the code resizes the image and uploads it to the list for a nice and consistent look and feel of your website.
Copy and paste the code below into your ItemAdded method.

       public override void ItemAdded(SPItemEventProperties properties)
int _imageWidth = 0;
int _imageHeight = 0;

if (properties.ListTitle.ToLower().Equals("intranet pictures"))
string _width = properties.ListItem.File.Properties["vti_lastwidth"].ToString();
string _height = properties.ListItem.File.Properties["vti_lastheight"].ToString();

if (Int32.TryParse(_width, out _imageWidth) && Int32.TryParse(_height, out _imageHeight))
//checking if the image height and weight is 120 (for the sake of example)
if (_imageWidth != 120 || _imageHeight != 120)
SPFile _imageFile = properties.ListItem.File;

MemoryStream _inputStream = new MemoryStream(_imageFile.OpenBinary(), true);
MemoryStream _outputStream = new MemoryStream();

Image _resizedImage = Image.FromStream(_inputStream).GetThumbnailImage(120, Int32.Parse(_height), null, IntPtr.Zero);
_resizedImage.Save(_outputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

_imageFile.SaveBinary(_outputStream, false);

catch (Exception ex)
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
//You can give the location of your custom error page here
properties.RedirectUrl = "/_layouts/Intranet_PictureRestrict/IntranetPicError.aspx";

Let me know if you want to customize it more or have any queries.

Cascading Dropdown in MOSS 2007

What is a Cascaded Dropdown?

A Cascaded dropdown is a combination of two dropdown lists. By selecting a value in the first one the options in the second get filtered according to its “parent” selection.
Sample uses:
Continents – Countries – Cities,
and many others ….

Solution :-

Download the Javascript here

Upload it to any document library in your site (assuming its hidden from users, you will not want users to modify it and give you a shock)

Right click on the javascript we just uploaded and click on “Copy Shortcut”

Now in the custom list New Form/Edit Form, add a Content Editor Webpart and make it hidden

Edit the WebPart properties and click on “Source Editor”

Paste the following line – 

void ParentDropDownList_SelectedIndexChanged(object sender, EventArgs e) 

     ChildDropDownListFieldControl child = (ChildDropDownListFieldControl)
               FindControlRecursive(this.Page, “ChildDropDownList”).Parent.Parent; 

There you go !!!

Hope this solution works for you.

Hide "Check In/Check Out" option from Context Menu using Content Editor WebPart (CEWP)

We often want to hide some options like Check In and Check Out from context menu. We can do this using a CEWP in the particular view.
Click on Site Actions > Edit Page
Click on Add a Web Part and select “Content Editor Web Part” from the options.
A CEWP will get inserted, click on the “edit” and select “Modify Shared Web Part“.
On the right you will see a “Source Editor” button, clicking which will open a text editor where you can write javascript code to be used in that particular view.
Copy and Paste the following Code to hide the options from context menu.

AddCheckinCheckoutMenuItem is a function used in the Core.js file, we are overridding the same function and doing no actions inside the same. Like the same way, we can hide any option from the context menu.