Programmatically add a folder to Sharepoint library and set permissions - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Programmatically add a folder to Sharepoint library and set permissions

Last post 06-21-2010 07:15 AM by elizas. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 01-14-2008 11:08 PM

    • Jochen
    • Not Ranked
    • Joined on 01-15-2008
    • Posts 4

    Programmatically add a folder to Sharepoint library and set permissions

    I am using this code to create a new folder in a sharepoint library and to set the permissions to this folder. The code works in infopath client but but as a webbased form I always get an error. I think it must be a security problem. The form is fill trusted.

    Code:

            public void btn_addFolder_Clicked(object sender, ClickedEventArgs e)        {                     XPathNavigator nav = MainDataSource.CreateNavigator();            XPathNavigator x = nav.SelectSingleNode("/my:XArgoEntry/my:username", NamespaceManager);            string text = x.Value;             XPathNavigator nav1 = MainDataSource.CreateNavigator();            XPathNavigator x1 = nav1.SelectSingleNode("/my:XArgoEntry/my:Number", NamespaceManager);            string foldername = x1.Value;             SPSite site = new SPSite("http://w2k3:3000/sites/Magna");            site.AllowUnsafeUpdates = true;            SPWeb web = site.OpenWeb("");            web.AllowUnsafeUpdates = true;             SPFolder folder = web.GetFolder("Dokumente");            SPFolder newfolder = folder.SubFolders.Add(foldername);             SPWebCollection collsite = site.AllWebs;                        //********** Userberechtigung setzen BEGINN ******            SPUser user = collsite[""].EnsureUser(text);             SPRoleDefinition RoleDefinitionRdr = collsite[""].RoleDefinitions.GetByType(SPRoleType.Reader);            SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)user);             roleAssignment.RoleDefinitionBindings.Add(RoleDefinitionRdr);             if (!newfolder.Item.HasUniqueRoleAssignments)            {                newfolder.Item.BreakRoleInheritance(false);            }             while (newfolder.Item.RoleAssignments.Count > 0)                newfolder.Item.RoleAssignments.Remove(0);            newfolder.Item.RoleAssignments.Add(roleAssignment);                         //******* Userberechtigung ENDE ********             newfolder.Item.Update();

    Error:

    Microsoft.SharePoint.SPException: Die Sicherheitsüberprüfung für diese Seite ist ungültig. Klicken Sie auf die Schaltfläche 'Zurück' Ihres Browsers, aktualisieren Sie dann die Seite, und starten Sie den Vorgang erneut. ---> System.Runtime.InteropServices.COMException (0x8102006D): Die Sicherheitsüberprüfung für diese Seite ist ungültig. Klicken Sie auf die Schaltfläche 'Zurück' Ihres Browsers, aktualisieren Sie dann die Seite, und starten Sie den Vorgang erneut. at Microsoft.SharePoint.Library.SPRequestInternalClass.UpdateRoleAssignment(String bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove) at Microsoft.SharePoint.Library.SPRequest.UpdateRoleAssignment(String bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove) --- End of inner exception stack trace --- at Microsoft.SharePoint.Library.SPRequest.UpdateRoleAssignment(String bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove) at Microsoft.SharePoint.SPRoleAssignmentCollection.UpdateAssignment(Int32 principalId, SPRoleDefinitionBindingCollection bindings, Boolean addOnly) at Microsoft.SharePoint.SPRoleAssignment.Update() at Microsoft.SharePoint.SPSecurableObjectImpl.BreakRoleInheritance(Boolean CopyRoleAssignments) at Microsoft.SharePoint.SPListItem.BreakRoleInheritance(Boolean CopyRoleAssignments) at XArgoEntryDev2.FormCode.btn_addFolder_Clicked(Object sender, ClickedEventArgs e) at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonEventHost.<>c__DisplayClass6.<>c__DisplayClassa.<add_Clicked>b__3() at Microsoft.Office.InfoPath.Server.Util.DocumentReliability.InvokeBusinessLogic(Thunk thunk) at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonEventHost.<>c__DisplayClass6.<add_Clicked>b__2(Object sender, ClickedEventArgs e) at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonEventHost.FireClickedEvent(Object sender, ClickedEventArgs args) at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonFormCode.<>c__DisplayClass2.<>c__DisplayClass5.<Click>b__1() at Microsoft.Office.InfoPath.Server.DocumentLifetime.OMExceptionManager.CallFormCodeWithExceptionHandling(UserMessages userMessages, OMCall d)
  • 01-15-2008 07:48 AM In reply to

    Re: Programmatically add a folder to Sharepoint library and set permissions

    Welcome to the forum.

    I do not believe you can have that type of code in a browser form and cannot have full trust.

    I would think you would need tolook at having some code on SharePoint that could maybe react to a new document and create the filing location instead.

  • 01-15-2008 09:14 AM In reply to

    Re: Programmatically add a folder to Sharepoint library and set permissions

    I searched for the error you are seeing (you can just Google for "0x8102006D UpdateRoleAssignment") and there might be a solution for you, please check out the following blog article:

    http://moldenco.blogspot.com/2007/05/spsecurityrunwithelevatedprivileges-to.html

    Good luck!

    Dave

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 01-15-2008 11:43 PM In reply to

    • Jochen
    • Not Ranked
    • Joined on 01-15-2008
    • Posts 4

    Re: Programmatically add a folder to Sharepoint library and set permissions

    I have tried doing this. But when I click my button I still get the error message. When I keep the open and click the button once more then it works.

    What could be the reason for this. Is there a way to solve this problem.

  • 02-09-2010 05:37 AM In reply to

    Re: Programmatically add a folder to Sharepoint library and set permissions

    SharePoint site security helps manage permissions for different resources within a site by defining the levels of accessibility permissions for different peoples and groups. In SharePoint, always the top-level or the parent level permissions are inherited to it's child contents (e.g. a sub-site inheriting permissions from it's parent site collection).In-order to create unique permission we need to break the inheriting parent permission and create new permission level for the SharePoint content. These permissions can be defined for specific users or groups. http://www.mindfiresolutions.com/
    Cheers,
    Eliza
  • 05-06-2010 06:41 AM In reply to

    Re: Programmatically add a folder to Sharepoint library and set permissions

    In SharePoint, View is a virtual representation of a set of data from a specific list. We can programmatically create and update views for a list. Provided below is an example in C#.Net which explains the approach of creating and updating views. The code is divided in to 2 parts i.e. Part I - describes how to create a view by assigning specific fields from a list, and Part II - describes how to update an existing view by adding few more fields to it. using (SPSite oSPsite = new SPSite("http://website_url/")) { oSPsite.AllowUnsafeUpdates = true; using (SPWeb oSPWeb = oSPsite.OpenWeb()) { oSPWeb.AllowUnsafeUpdates = true; /* get the list instance by list name */ SPList list = oSPWeb.Lists["List_Name"]; /* ======== Part I (create a view) ======== */ // add the field names to the string collection StringCollection strViewFields = new StringCollection(); strViewFields.Add("FullName"); strViewFields.Add("Address"); strViewFields.Add("City"); strViewFields.Add("State"); http://www.mindfiresolutions.com/sharepoint-development.htm
    Cheers,
    Eliza
    Filed under: , ,
  • 06-21-2010 07:15 AM In reply to

    Re: Programmatically add a folder to Sharepoint library and set permissions

    In SharePoint, View is a virtual representation of a set of data from a specific list. We can programmatically create and update views for a list. Provided below is an example in C#.Net which explains the approach of creating and updating views. The code is divided in to 2 parts i.e. Part I - describes how to create a view by assigning specific fields from a list, and Part II - describes how to update an existing view by adding few more fields to it. using (SPSite oSPsite = new SPSite("http://website_url/")) { oSPsite.AllowUnsafeUpdates = true; using (SPWeb oSPWeb = oSPsite.OpenWeb()) { oSPWeb.AllowUnsafeUpdates = true; /* get the list instance by list name */ SPList list = oSPWeb.Lists["List_Name"]; /* ======== Part I (create a view) ======== */ // add the field names to the string collection StringCollection strViewFields = new StringCollection(); strViewFields.Add("FullName"); strViewFields.Add("Address"); strViewFields.Add("City"); strViewFields.Add("State"); http://www.mindfiresolutions.com/Create-and-Update-Views-Programmatically-in-SharePoint-451.php
    Cheers,
    Eliza
    Filed under: , ,
Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.