Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

what would be the best way of getting SPWeb and List in context below, specially when list title can be different for different cultural websites,

   private bool doesItHaveIt(SPItemEventProperties properties)
            {
                using (SPWeb web = properties.OpenWeb())
                {
                    SPList DocLib= web.Lists["Doc Library"]; //its list where item is being added

                    if (DocLib.ItemCount < 1)
                    {

and also what would be best way of getting resources from 12 hive,

Culture ci = null;
string listName;

try
{
   using (SPWeb web = properties.OpenWeb())
   {
       ci = web.Locale;
       listName = Resources.ListNames.ResourceManager.GetString("MyList", ci);
share|improve this question

2 Answers 2

up vote 2 down vote accepted

first of all, SPItemEventProperties has Web method, that you can use. If I'm not wrong, you do not need to dispose this object. Second, use SPUtility.GetLocalizedString.

share|improve this answer
    
SPItemEventProperties.Web property isn't provided in sharepoint 2007 am afraid :( –  Muhammad Raja Dec 6 '12 at 14:27

why dont you follow msdn as its the first place i look for best practices. This is for spweb in an event reciver ;) also your doesItHaveIt(SPItemEventProperties properties) already handels a web object in properties so there is no need to dispose of the object.

http://msdn.microsoft.com/en-us/library/ee724407(v=office.14).aspx

good

// Retrieve SPWeb and SPListItem from SPItemEventProperties instead of
// from a new instance of SPSite.
SPWeb web = properties.OpenWeb();
// Operate on the SPWeb object.
SPListItem item = properties.ListItem;
// Operate on an item.

bad

public override void ItemDeleting(SPItemEventProperties properties)
{
    using (SPSite site = new SPSite(properties.WebUrl))
    {
    using (SPWeb web = site.OpenWeb())
        {
        SPList list = web.Lists[properties.ListId];
        SPListItem item = list.GetItemByUniqueId(properties.ListItemId);
        // Operate on an item.
        }
    }
}

for your resource string:

SPUtility.GetLocalizedString("$Resources: GetAllitems", "core", 1033);

it translates as, get GetAllitems from core.resx with the language of en-US (english) which makes the file core.en-US.resx within web server extensions\12\Resources

as alexander pointed out from msdn:

sourceType:

System.String An ASP.NET resource expression in the form $Resources:keyname, where keyname is the name half of a name/value pair in a resource file (.resx).

defaultResourceFileType:

System.String The base file name of the language resource file containing a localized string value. For example, if you have a series of resource files named myresources.en-us.resx, myresources.es-es.resx, myresources.de-de.resx, and so on, the value to pass in this parameter is myresources.

languageType:

System.UInt32 The LCID of the target language. For a list, see the overview topic for the CultureInfo class.

hope it helps :)

share|improve this answer
    
I think it's better to dispose that SPWeb object you retrieved using OpenWeb(). –  Norbert Dec 6 '12 at 16:09
    
NO you do not dispose of it as its handled already! If you dispose of it then the anything after will cause an error. After executing the code other event receivers will receive an object where the underlaying SPWeb object has been disposed. This can lead to exceptions. You only dispose SPSite and SPWeb objects that your code owns, do not dispose spweb/site that raised this event. –  ali Sharepoint Dec 6 '12 at 16:22
4  
"Event Receivers and properties.OpenWeb() WSS 3.0: When you call properties.OpenWeb() the returned SPWeb will need to call Dispose() SharePoint Foundation 2010: Use the newly introduced SPItemEventProperties.Web property instead of SPItemEventProperties.OpenWeb() for better performance and to avoid the need to call Dispose()". source –  Norbert Dec 6 '12 at 17:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.