[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SPSecurity.RunWithElevatedPrivileges

Posted on 2008-10-16
8
Medium Priority
?
3,929 Views
Last Modified: 2010-04-21
Please see the code below.  

I want to change it so that the Sub CreateDoc is a function instead of a sub routine

Function CreateDoc(strfolderName) as guid
...
End Function

But, I cannot pass the function in the SPSecurity.RunWithElevatedPrivileges(AddressOF CreateDoc).

How do I do this so that I can have the createdoc as a function so that it is more flexible

'*** START CURRENT CODE***
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     SPSecurity.RunWithElevatedPrivileges(AddressOf createdoc)
End Sub
 
Sub Createdoc()
     Dim site As SPSite = New SPSite("http://mysharepointsite/")
     Dim web As SPWeb = site.OpenWeb()
     web.AllowUnsafeUpdates = True
     Dim doclibname As String = "Document Libary2"
     Dim guid As System.Guid = web.Lists.Add(doclibname, "Document Libary2", SPListTemplateType.DocumentLibrary)
    End Sub
 
'*** END CURRENT CODE***
 
 
**BEGIN NEW CODE ***
Function Createdoc(ByVal DocLibName As String) As Guid
        Dim site As SPSite = New SPSite("http://mysharepointsite/")
        Dim web As SPWeb = site.OpenWeb()
        web.AllowUnsafeUpdates = True
        Dim guid As System.Guid = web.Lists.Add(DocLibName, "Document Libary2", SPListTemplateType.DocumentLibrary)
        Return guid
    End Function

Open in new window

0
Comment
Question by:cartch2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 22732859
The MSDN documentation for RunWithElevatedPrivileges states that;

"The secureCode object [that's the arg that specifies your secure method] can be created from any method that is parameterless and returns void"

It's a condition that no arguments are required, and no return value exists.

You therefore cannot turn CreateDoc into a Function without breaking these requirements.
0
 

Author Comment

by:cartch2008
ID: 22732934
do you know how I can create a document libary by being able to pass in the document library name I want and getting the id of that document library that was created back?
0
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 22733012
I'm just heading out, so this is a rushed response!

Could you communicate via a Global Variable, Session variables, or capture the event when the doc lib is created?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:cartch2008
ID: 22733318
I could use a session var, but, there has got to be a better way to do this
0
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 22733808
The last time I wanted to get some data out of an asynchrnous delegate I got it to write the data to Session.

The delegate in your case isn't asynchronous but the lesson holds.

Set up a name value pair. The delegate method will obtain the details of the document library to create from there, and write the creation details to the same entry.

The amount of data involved is very small and will not bloat the Session storage even with loads of users.

I'm looking at the C# code I used now, and it's as easy as it sounds. Sometimes the straightforward solution is the best.
0
 

Author Comment

by:cartch2008
ID: 22733887
can you provide an example?
0
 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 total points
ID: 22734432
This code is way more complicated than you need because it's asynchronous and events are used to write to Session. It could be made as simple as;

string sessionKey = "NewDocLibName|NONE";

Delegate;
foreach (string s in Session.Keys)
{
    if (s.Contains("NONE"))
    {
         Create doclib;
         string newKey = s.Replace("None", "doclibID");
         Session.Remove("s");
         Session.Add(newKey);
    }
}

That is very rough I know, but I'm in the middle of something at home!!
0
 

Author Closing Comment

by:cartch2008
ID: 31506809
Thanks!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question