Programmatically uploading and setting permission to a document in a document library in SharePoint


We can use the following code for uploading a document to document library.

// create a filestream object and open an existing file for reading its contents

FileStream oFileStream = File.OpenRead(@”C:\G.doc”);

byte[] content = new byte[oFileStream.Length];

// store the contents in a byte array

oFileStream.Read(content, 0, (int)oFileStream.Length);

oFileStream.Close();

// create an SPSIte object

using (SPSite objSite = new SPSite(“http://d-1246”))

{

// get reference to the web application or web site

using (SPWeb objWeb = objSite.OpenWeb())

{

// get reference to the document library. Proposal – name of the document library

SPFolder mylibrary = objWeb.Folders[“Proposal”];

objWeb.AllowUnsafeUpdates = true;

// add the file to the document library and update it

SPFile myFile = mylibrary.Files.Add(“MyProposal.doc”, content, true);

mylibrary.Update();

// get the reference to the file just uploaded as a SPListItem for setting permissions

SPListItem myListItem = myFile.Item;

// HasUniqueRoleAssignments – returns false if it inherits permission from parent else true

myListItem.HasUniqueRoleAssignments.ToString());

// BreakRoleInheritance – breaking role inheritance so that we can define our own permissions

myListItem.BreakRoleInheritance(false);

// webroledefinitions – Full Right, Design, Contribute and Read

SPRoleDefinitionCollection webroledefinitions = objWeb.RoleDefinitions;

// group – different groups created in that site

SPGroup group = objWeb.SiteGroups[“MyGroup”];

SPRoleAssignment roleassignment = new SPRoleAssignment(group);

roleassignment.RoleDefinitionBindings.Add(webroledefinitions[“Design”]);

myListItem.RoleAssignments.Add(roleassignment);

// same for the user as well

SPUser user = objWeb.SiteUsers[@”domainname\username”];

SPRoleAssignment roleassignment1 = new SPRoleAssignment(user);

roleassignment1.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);

myListItem.RoleAssignments.Add(roleassignment1);

}

}

Bye

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

10 thoughts on “Programmatically uploading and setting permission to a document in a document library in SharePoint”

  1. Hi,

    Im looking out for a script which
    automates the archival policy processes.

    I would have a dump of documents and an excel sheet provided with the following columns:
    Old File Name
    New File Name
    Folder Name (where files need to be moved)
    Ad–ID to provide access at files

    Script should read the information from the excel sheet to perform the following actions into WSS:

    · Re-naming of files as per the new file name mentioned in the sheet.

    · Movement of files to a particular folder as per the folder name mentioned in the sheet.

    · Automatic access to the files as per the Ad-Id mentioned in the sheet.

    System should validate the access of the document for each user.

    I would be grateful if you could assist me on this

    Thanks & Regards,
    narayanan rm.

    Like

  2. The topic is to much informative but what we have to do if i have a document already uploded..and i want a refrence a that file i.e abc.doc.Please help me if someone have knowledge about it.
    Thanks in Advance

    Like

  3. thank you so much. The post was helpgful. I have another question. I have a folder and a subfolder inside that which is hiiden and the file inside the subfolder. I need to access the worflow status column of the file and update it in the folder workflow column. Any help in this is appreciated

    Like

  4. Thanks for your post.I want to know if it is possible to create application from sharepoint that can integrate outlook 2007 by creating alert programatically.

    Like

  5. This is the easiest way of usage, but a fairly good example.
    Suggest to extend this with using SPItemEvent, f.e. ItemUploaded, add som extra, f.e. RunWithElevated privileges and AD group handling, not just trivial SPGroup.
    This way the sample would be more realistic.

    Like

  6. by applying this method permissions are set on item not on document , if user who has read permission can download document , edit the document and saved the changes to server ,

    Like

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: