Link to home
Start Free TrialLog in
Avatar of cartch2008
cartch2008

asked on

SPSecurity.RunWithElevatedPrivileges

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

Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

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.
Avatar of cartch2008
cartch2008

ASKER

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?
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?
I could use a session var, but, there has got to be a better way to do this
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.
can you provide an example?
ASKER CERTIFIED SOLUTION
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!