Get SPWeb from Site or from SPContext

Hi,
i am trying to get the SPWeb object,
i can get it using the Site:
Statically 
using (SPSite site = new SPSite("http://url/"))
               using (SPWeb web = site.OpenWeb("NameOfSubSite"))
but using this way i should enter the URL of site when get the SPSite and the name of subsite when get SPWeb using
site.OpenWeb
Dynamically
by use the SPContext: SPWeb web = SPContext.Current.Web;
here i can get the current web without enter the URL of site nor the name of subsite
the question is what is the best way to get the web of current site, and what is the difference between the 2 ways.
Thank you
Regards

hi,
One of the main differences is that in the SPContext case you don't own the SPSite object, so it can stop existing at any time (and thus you would get an SPException kindly letting you know that the object was already closed), whereas creating the instance
yourself makes you the owner and responsible for its disposal.
Sometimes, when you develop controls used locally as in a WebPart, using SPContext's SPSite is enough. However, there are many scenarios where this is not the case: when you need to target another SPSite rather than your own, or perhaps when you need
to develop an asynchronous process (say, within a SharePoint timer --SPJobDefinition--). Also, I recently read something about problems arising when using SPContext's SPSite object within a method called by SPSecurity.RunWithElevatedPrivileges.
Personally, as a rule of thumb, I use SPContext.Current.Site (or SPContext.Current.Web for that matter) when developing WebParts or Web Controls whose purpose is to read data from an SPList or something like that. Otherwise (as in the timers) I create my own
instance of SPSite.
Whenever you see a reply and if you think is helpful,Vote As Helpful! And whenever you see a reply being an answer to the question of the thread, click Mark As Answer

Similar Messages

  • Firefox connects to google-analytics for some sites, and then gets stuck, and I can never get onto the site. How do I prevent Firefox from connecting to google-analytics? I use XP with service pak 3 in English

    Firefox connects to google-analytics for some sites,when i click on links in websites or emails and then gets stuck, and I can never get onto the site. Or opens a new bower behind the one i'm usind that's blank i don't even know it's there untill i close the one i'm using. How do I prevent Firefox from connecting to google-analytics? Or opening a blank bowser behind the one i'm using. I use XP with service pak 3 in English
    == This happened ==
    A few times a week
    == a couple months ago

    I got the same issue.
    I go on a website and all the sudden another window pops up with "results.google-analytics.com" or "search.google-analytics.com". It has often ads for other sites for example. black single dating site
    how can I can I stop that from happening again?
    I didn't download or do anything, just visit websites, that I visit on a regular basis.
    OS: Windows XP
    Firefox Version: 3.6.6

  • Re; Getting data from a live web site, based on geo-location.

    re; Getting data from a live web site, based on geo-location.
    Approx 4 minutes 25 seconds into this YouTube Adobe ELearning video for Common JavaScript Interface in Adobe Captivate 8, the topic of extracting data from a live web site http://api.openweathermap.org
    Is there anywhere we can download this Adobe Captivate weather ticker project? or get some more complete tutorial on setting it up?  I tried to copy some of the code shown in the video, but can't get it to work, so I suspect I'm missing something somewhere.
    I would like to try it out.
    Video Here:   https://www.youtube.com/watch?v=u1ZfG47k24k

    Could windows Firewall be stopping this geo-location weather ticker app from working? When I start Captivate 8, I get the message indicating the the firewall is blocking some features of the program. When I check the geo var it seems empty.
    Name: AdobeCaptivateWS
    It then refers to some temp folder such as
    Path: C:\users\{username}\appdata\local\temp\cp3320604394session\cptrustfolder3320604659\adobec aptivatews
    Network Location: Public, private, domain networks
    etc...

  • I have an iPod touch and it doesn't seem to be working right it is rather slow I get booted from sites I have deleted history cache cookies still have 22.1 available space what can I do to solve the problem do I need an antivirus

    I have an iPod touch that does not seem to be working properly I have deleted history cache cookies I gave 22.1 available I get booted off sites for no reason do I need an antivirus if so where can I find a good free one

    There are no antivirus apps for any iDevice
    - Try the standard fixes:
    - Reset. nothing is lost
    Reset iPod touch: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Restore from backup
    - Restore to factory defaults/new iPod.

  • Get data from Sharepoint site in different farm using webservice, and current site using claims mode authentication.

    Hi , 
    I have a site that runs on   Claims Mode (  NTLM) . That site has a web part that needs to show the data from any  sharepoint farm, SharePoitn  2010 , or 2007 or 2003.
    I am getting 401 unauthorized when I   try to get data from webservice running in sharePoint context.
    But when I run the same code in  Windows Console application  then it is giving no error.
    What I doubt is that this issue is due to the reason that I have  set
    claims mode authentication.
    Because same code is running  in different farm in a site that is configured  using windows authentication.

    So generally speaking, you're talking about a VERY long running topic of authentication methods... and generally speaking, in the world of Windows, the only long running authentication options have been:
    - NTLM (limited to one hop)
    - Kerberos (unlimited hops)
    - Application level authentication (ex: SQL auth, also, no hops)
    Recently, Microsoft has been investing in Claims Based Auth... and I fully expect claims to start working their way into other systems (previously starting to work into Windows via the CardSpace technology, but also in other ways such as Win8's ability to
    log in with a LiveID)... but building a new authentication method into ALL applications is a VERY long process, and the complexity of claims adds a LOT of consideration (claims from the same AD can look VERY different depending on the STS, so lots of questions
    around things like bridging claims).
    So as far your SP auth needs...
    IF both applications are CLAIMS AWARE, then you MAY be able to use claims (which support unlimited hops)... but that's just not very likely yet (and will probably take another 5-10 years to be available across the entire enterprise)... otherwise, KERBEROS
    Outside of the Microsoft world... KERBEROS is open spec, so is supported by other systems (such as authenticating to linux)... claims based auth is also open spec, but again, still new... there are a few other options (LDAP, etc), but none that are native
    to Windows (so you rely on things like third party auth modules for Windows, which Novell has done for DECADES with NDS and eDir)
    And again, SharePoint can convert claims to Kerberos using the C2WTS... which MS uses internally for things Excel Services connecting to a backend SQL server... but it DOES require the Kerb and C2WTS configuration.
    if you're having issues using Kerb auth... then it sounds like Kerb isn't configured correctly... and Kerb is a PAIN to configure (whitepaper for SP Kerb is ~100 pages long)... IIS (and SharePoint) also has the added benefit of failing over to NTLM if Kerb
    fails (and you shouldn't disable this behavior, since it'll break other settings elsewhere)
    Scott Brickey
    MCTS, MCPD, MCITP
    www.sbrickey.com
    Strategic Data Systems - for all your SharePoint needs

  • Fastest way to get data from Multiple lists across multiple site collections

    HI
    I need to get data from multiple lists which spread across 20 site collections and need to show it as list view.
    I have searched on internet about this and got some info like options would be to use search core APIs or BCS . I can't use search because I want real time data. Not sure of any other ways.
    if anybody can provide ideas it would be help.

    Might LINQ be an option for you?  Using
    LINQPad and the
    SharePoint Connector, you should be able to write a query that'll retrieve this data, from which you can tabulate it.  I'm not sure how you'd be able to automate this any further so that it's then imported in as list.
    For something more specific, I used a third party tool called the
    Lightning Tools Lightning Conductor, which is essence a powerful content roll-up tool.  In one of my solutions, I created a calculated column that gave an order / ranking on each item, so that when lists were combined, they'd still have some form of
    order.  The web part is also fairly customisable and has always proven a useful tool.
    Hope that helps.
    Steven Andrews
    SharePoint Business Analyst: LiveNation Entertainment
    Blog: baron72.wordpress.com
    Twitter: Follow @backpackerd00d
    My Wiki Articles:
    CodePlex Corner Series
    Please remember to mark your question as "answered" if this solves (or helps) your problem.

  • Is there any REST API to get all the Site and site Collections from Sharepoint Online

    Hi,
    I have one SharePoint Online tenant. We are using REST API to integrate SharePoint to our internal System.
    For my use case I need to get all the SharePoint sites ans Sitecollections. From each SiteCollection I need all the subsites under this.
    I have used below REST Query
    http://<Base site url>/_api/web
    This doesn't give me all the Site collections
    Could any one please give me some way to get all the Site collections and sites from my sharepoint tenant through REST API.
    Thanks,

    Hi Abhiram,
    There seems no such endpoint in SharePoint REST API can retrieve a list of site collections in a SharePoint Online tenant:
    https://msdn.microsoft.com/en-us/library/office/jj860569.aspx#Reference
    If Client Object Model can be an option for you, you can use Tenant object to achieve what you want:
    http://social.technet.microsoft.com/wiki/contents/articles/24050.perform-basic-admin-operations-using-csom-in-sharepoint-online.aspx#Scenario_3_Fetch_All_Site_Collections
    Anyway, I would suggest you submit a feedback to the Office Developer Platform if there any expectation about the future version of REST API of Office 365 SharePoint
    Online:
    http://officespdev.uservoice.com/
    It
    is a place for customers provide feedback about Microsoft Office products. What’s more, if a feedback is high voted there by other customers, it will be promising that Microsoft
    Product Team will take it into consideration when designing the next version in the future.
    Thanks 
    Patrick Liang
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • What update site flexPMD plugin for FlashBuilder 4.6 ? and login/password to get it from SVN ?

    What update site flexPMD plugin for FlashBuilder 4.6 ? and login/password to get it from SVN ?

    Just thought I'd answer here to tell you what's going on around FlexPMD.
    FlexPMD has been donated to the Apache Flex project and has been sitting there since then. In invested quite some time to find out if we would release FlexPMD soon. Even if if does work when using the Ant target the Maven part is completely out of date and I didn't manage to get things working in reasonably up-to-date Maven versions. It seems that FlexPMD had been a fork of the original PND which hasn't been updated for a really long time. Instead of fixing and updating FlexPMD to work on Maven again, I contributed a patch to PMD that helped modularize PMD 5.2. This has been released recently and even the maven-pmd-plugin for that version have now been releases. So this version allows to create a Flex language module for the official PMD system. Hopefully with this we'll get a well maintained PMD core to work on top of.
    Unfortunately I had to finish some other things first, but I do have this on my radar next. So please subscribe to the flex user list and you'll be kept up to date on any progress.
    Chris

  • With the do not track feature checked I still get ads from sites visited soon after leaving these sites. What to do?

    I have checked the box for do not track but get ads from sites visited after leaving these sites. These ads run parallel on the Yahoo home page. What I can I do about this?

    That feature relies upon voluntary compliance to your preference by websites.
    https://support.mozilla.com/en-US/kb/how-do-i-stop-websites-tracking-me

  • Hi, I'm moving my site from MobileMe to Filezilla and there is apparently a coding error on iWeb that means when I try to get onto my site there's no access online and it says there are too many redirects. Anyone have any idea what this might be?

    Hi, I'm moving my site from MobileMe to Filezilla and there is apparently a coding error on iWeb that means when I try to get onto my site there's no access online and it says there are too many redirects. Anyone have any idea what this might be?

    Well, firstly Filezilla is not a hosting company but an ftp client that helps you upload your site to your new server. You need to purchase a new hosting account first though.
    If you have iWeb 09 then you can publish directly to your new host and you select the publish to ftp option and not MobileMe - MobileMe won't be here after tomorrow.
    You can also publish to a local folder if want to and use Filezilla to upload your site to your server.
    You don't need re-directs as MobileMe won't be here so you need to cancel and delete your domain name from your MobileMe account and then cancel any domain name forwarding that you have at your domain name registrar - that is what the redirects are about.
    Firstly ensure that you have your new hostings account - Filezilla is not a host, rather an ftp client that you use to upload your site.

  • Do I need to enable MySite or have a MySite created in order to follow content and get updates from Team Site library/Newsfeed?

    Hello, we are migrating to SharePoint 2013. In the current SharePoint 2007 implementation we have disabled MySite for several reasons. Question: Do we need to enable MySite or have a MySite created in order to follow content and get updates from Team
    Site library/Newsfeed?

    Hi,
    According to your description, my understanding is that you want to know if the MySite is needed for following content and getting updates of the followed content.
    When users follow documents, sites, or tags, status updates from documents, conversations on sites, and notifications of tag use show up in users’ newsfeed, so users need to their MySite to view the updates in Newsfeed by OOB and you need
    to migrate the MySite to SharePoint 2013.
    However, we can also use APIs to programmatically follow content, please refer to the link below:
    http://msdn.microsoft.com/en-us/library/office/jj163217(v=office.15).aspx
    Best regards.
    Thanks
    Victoria Xia
    TechNet Community Support

  • Can my MacPro get viruses from explicit sites

    I just wanted to know If my Macbook Pro can get any viruses from viewing **** sites? I've heard from many people that Apple computers cannot get viruses, and from looking it up on google, i've heard that they can get viruses from them.

    Mac OS X versions 10.6.7 and later have built-in detection of known Mac malware in downloaded files. The recognition database is automatically updated once a day; however, you shouldn't rely on it, because the attackers are always at least a day ahead of the defenders. In most cases, there’s no benefit from any other automated protection against malware.
    The most effective defense against malware is your own intelligence. All known malware that affects a fully-updated installation of Mac OS X 10.6 or later takes the form of trojans, which can only work if the victim is duped into running them. If you're smarter than the malware attacker thinks you are, you won't be duped. That means, primarily, that you never install software from an untrustworthy source. How do you know a source is untrustworthy?
    Any website that prompts you to install a “codec,” “plug-in,” or “certificate” that comes from that same site, or an unknown site, merely in order to use the site, is untrustworthy.
    A web operator who tells you that you have a “virus,” or that anything else is wrong with your computer, or that you have won a prize in a contest you never entered, is trying to commit a crime with you as the victim.
    “Cracked” versions of commercial software downloaded from a bittorrent are likely to be infected.
    Software with a corporate brand, such as Adobe Flash Player, must be downloaded directly from the developer’s website. No intermediary is acceptable.
    Disable Java (not JavaScript) in your web browser(s). Few websites have Java content nowadays, so you won’t be missing much. This setting is mandatory in Mac OS X 10.5.8 or earlier, because Java in those versions has bugs that make it unsafe to use on the Internet. Those bugs will probably never be fixed, because those older operating systems are no longer being maintained by Apple. Migrate to a newer version of the Mac OS as soon as you can.
    Follow these guidelines, and you’ll be as safe from malware as you can reasonably be.
    Never install any commercial "anti-virus" products for the Mac, as they all do more harm than good. If you need to be able to detect Windows malware in your files, use ClamXav — nothing else.

  • Clients getting updates from Primary Site~~~SCCM 2012 R2

    Hi Guys,
    I have one Primary site and 3 DP, And there are lots of branch and we have enable branch Cache using GP.
    But i have observed some of my clients not getting updates from respective DP they are directly coming on primary site  for updates. In my environment ADR is running for updates.  
    My boundaries groups and boundaries correctly configured .
     Thanks

    boundary  active site wise setup...
    In  client
    LocationServices.log DP details not showing .

  • Question for get information from https site with socket.

    I had tried to send a HTTP request with socket like :
    POST http://xxx.xxx..xxx/bbs/bbsadd.php?blockid=119&title=Hello&content=Hello&clid=101&Ok=OK&s_no=0&mood=1 HTTP/1.0\r\n\r\n"
    Just like this way , I could get some information or submit any message to http site like that .
    But that was not success to get some information for visited https ( like Yahoo ) use the same way .
    I had read some document about the https protocl I knew that had to need SSL for validate .
    But what should I do as a socket program ?
    package sailing;
    import java.net.*;
    import javax.net.ssl.*;
    import java.io.*;
    public class ReceiveMail
         //private Socket toYahoo;
         private SSLSocket toYahoo;
         private SSLSocketFactory factory;
         private BufferedReader read;
         private DataOutputStream write;
         private String login;
         public ReceiveMail(String userName,String userPassword)
              try
                        //this.toYahoo=new Socket("202.43.216.165",80);
                        this.factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
                        this.toYahoo=(SSLSocket)this.factory.createSocket("202.43.216.165",443);
                        //this.login="POST https://edit.bjs.yahoo.com/config/login?.src=ym&login="+userName+"&passwd="+userPassword+" HTTP/1.0\r\n\r\n";
                        this.login="POST https://edit.bjs.yahoo.com/config/login?.src=ym&login="+userName+"&passwd="+userPassword+" HTTP/1.0\r\n\r\n";
                        byte[] makeBytes=this.login.getBytes();
                        this.write=new DataOutputStream(this.toYahoo.getOutputStream());
                        this.write.write(makeBytes,0,makeBytes.length);
                        this.write.flush();
                        this.read=new BufferedReader(new InputStreamReader(this.toYahoo.getInputStream()));
                        String readRes="";
                        String webFileCode="";
                        while((readRes=this.read.readLine())!=null)
                             webFileCode+=readRes;
                        System.out.println(webFileCode);     
              catch(IOException e)
                   System.out.println(e);
         public static void main(String args[])
              new ReceiveMail("sailing","sailing");
    }================================
    If this program perform successful , I think it will print code that login Yahoo Mail success .
    But I just could get an error code .................
    Why?

    Hi,
    According to your post, my understanding is that you wanted to get information from multiple lists once a user was selected.
    There is no out of the box way to accomplish this with SharePoint.
    As a workaround, I recommend to create a list to store the user names. Then you can create relationships between lists using the browser UI. Once you select the user name in the parent list, the associated items will be displayed in the child lists.
    Here are two great articles for you to take a look at:
    http://office.microsoft.com/en-in/sharepoint-server-help/create-list-relationships-by-using-unique-and-lookup-columns-HA101729901.aspx#_Toc270607416
    http://msdn.microsoft.com/en-us/library/ff394632.aspx
    Best Regards,
    Linda Li
    Linda Li
    TechNet Community Support

  • When i want to download firefox, it gets downloaded from a different site everytime. Is this legit?

    when i want to download firefox, it gets downloaded from a different site everytime. Is this legit?
    For example: a box from Norton Antivirus will pop up to ask me if I want to download firefox from ______ . The line will be filled with a different site everytime (this is one site: saimei.acc.umu.se). Can I trust this?

    Yes as there is not one server but several mirrors around the help load balance the releases.mozilla.org
    http://www.mozilla.org/community/mirrors.html
    Many of the mirrors are Universities and also help host for other open source stuff like various Linux distros.
    The one you link to is a University in Sweden.

Maybe you are looking for