استخدم WebPart على مجموعة متنوعة لقراءة قائمة في الموقع العلوي

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/89761

سؤال

لقد قمت بإنشاء قائمة إدارة SharePoint، حيث أنقذت مجموعة متنوعة من الأشياء ل WebPart. يمكن للمديرين فقط قراءة القائمة، لقد أزلت حقوق المستخدم من أي شخص آخر.

يقرأ WebPart التالي القائمة باستخدام امتيازات مرتفعة. giveacodicetagpre.

ومع ذلك، عندما أقوم بإنشاء مجموعة فرعية جديدة وأضيف WebPart إلى الموقع، لا يعمل، لأن تلك الفقرة الفرعية لا تحتوي على "قائمة إدارة".

سؤالي هو:

كيف يمكنني قراءة القائمة، إذا كان WebPart على صفحة مختلفة؟ ما هي التغييرات التي أحتاجها لجعلها؟

هل كانت مفيدة؟

المحلول

use open web, put in the site name for that web, so if i have:

SPSite/SPWeb

mysitecollection.com/mysubsite

it would be:

site.OpenWeb("mysubsite")

full code:

Guid siteID = SPContext.Current.Site.ID;
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(siteID))
        {
        // List 'AdminList' does not exist at site with URL 'http://devmach/sites/Test/Subsite'.
            using (SPWeb web = site.OpenWeb("mysubsite"))
            {
                SPList listsp = web.Lists["AdminList"];
                SPListItemCollection collListItems = listsp.Items;
                foreach (SPListItem itemsp in collListItems)
                {

the code should only be used to retrive the list, if you want to use the list in many subsites than create it in the sitecollection rather than a subsite!

Guid siteID = SPContext.Current.Site.ID;
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(siteID))
        {
        // List 'AdminList' does not exist at site with URL 'http://devmach/sites/Test/Subsite'.
            using (SPWeb web = site.OpenWeb())
            {
                SPList listsp = web.Lists["AdminList"];
                SPListItemCollection collListItems = listsp.Items;
                foreach (SPListItem itemsp in collListItems)
                {

first method advanced:

If its a requirement that you need the list within a subsite and need to access it within another subsite than this will do the trick:

get the list from the given subsite you have it on:

private SPList getlistFromSite(SPSite site)
{
                SPList listsp = null;
                using (SPWeb web = site.OpenWeb("mysubsite"))
                {
                    SPList listsp = web.Lists["AdminList"];
                }
                return listsp;
}

to use method above but keeping the current site the user is on!:

Guid webID = SPContext.Current.Web.ID;
Guid siteID = SPContext.Current.Site.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteID))
    {
        using (SPWeb web = site.AllWebs[webID])
        {
            SPList listsp = getlistFromSite(site);
            SPListItemCollection collListItems = listsp.Items;
            foreach (SPListItem itemsp in collListItems)
            {
                //loop
            }
        }
    }
});

نصائح أخرى

You have to know the website that contains AdminList.

Here are two ways to go:

  1. Create AdminList in the root web of the site collection and then in your code get the webID of the root web

    Guid webID = SPContext.Current.Site.RootWeb.ID;
    Guid siteID = SPContext.Current.Site.ID;
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(siteID))
        {
            using (SPWeb web = site.OpenWeb(webID))
            {
                SPList listsp = web.Lists["AdminList"];
                SPListItemCollection collListItems = listsp.Items;
                foreach (SPListItem itemsp in collListItems)
                {
    
  2. Add custom property to you webpart so you can set the Url of the list in the webpart's property pane, and use that property in your code to get the list.

    http://msdn.microsoft.com/en-us/library/dd584174(v=office.11).aspx http://zimmergren.net/technical/how-to-custom-web-part-properties-toolpart

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top