Solved

SPSecurity.RunWithElevatedPrivileges

Posted on 2008-10-16
8
3,822 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
  • 4
  • 4
8 Comments
 
LVL 31

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 31

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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

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 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net checkbox 7 41
SQL Exceptions 3 37
Ajax Panel Not Updating When Triggered By Timer 4 40
converting visio 2010 to powerpoint 2010  - formatting issues 5 34
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now