设置列表字段为主键

news/2024/7/7 19:25:54

转贴:Sample event handler to set a field as a pr imary key (enforce no duplicates)

Got this as a request from a reader- how to prevent users from adding items with same titles as ones that already exist in the list.

Code
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SPSTIPS.SharePoint.EventHandlers
{
    
public class TitlePrimaryEventHandler:SPItemEventReceiver
    
{
        
const string TITLE_QUERY = @"<Query><Where><Eq><FieldRef Name=""Title"" /><Value Type=""Text"">{0}</Value></Eq></Where></Query>";
        
public override void ItemAdding(SPItemEventProperties properties)
        
{
            
if (properties.AfterProperties["Title"!= null)
            
{
                
//get the title of the new item
                string currentTitle = properties.AfterProperties["Title"].ToString();
                
//get the web site object
                using (SPWeb web = properties.OpenWeb())
                
{
                     
//get the current list
                    SPList list = web.Lists[properties.ListId];
                    
//query the list to check if there are items with the same title
                    SPQuery q = new SPQuery();
                    q.Query 
= string.Format(TITLE_QUERY, currentTitle);
                    SPListItemCollection itemsWithSameTitle 
= list.GetItems(q);
                    
//if there are items, cancel the add, and show an error to the user.
                    if (itemsWithSameTitle.Count > 0)
                    
{
                        properties.Cancel 
= true;
                        properties.ErrorMessage 
= "There is already an item with the title \"" + currentTitle + "\" in this list\".";
                    }

                }

            }

        }

    }

}



上面的代码中,已经写了使用方法,就是用SPWeb web = properties.OpenWeb()

转载于:https://www.cnblogs.com/LifelongLearning/archive/2008/03/23/1118504.html


http://lihuaxi.xjx100.cn/news/254273.html

相关文章

Largest Rectangle in a Histogram

ps&#xff1a;单调栈&#xff0c;注意红色部分的代码。 int n;stack<P> s;inline void upd(LL &x, LL y) { (x < y) && (x y); }int main() {while(sc(n) ! EOF && n) {while(!s.empty()) s.pop();LL ans 0;Rep(i, 1, n) {int x;sc(x);if (s.e…

AI大神LeCun深度学习公开课来啦!4万字干货笔记

Datawhale干货 主讲&#xff1a;Yann LeCun&#xff0c;整理&#xff1a;新智元【新智元导读】Yann LeCun大师课程&#xff0c;搭配120页笔记食用效果更佳。喜欢深度学习&#xff1f;最好的方法就是在线课程。这里推荐图灵奖得主、纽约大学教授Yann LeCun主讲的在线课程。该课程…

云淘金时代,安全为王

在数字经济和技术生态高质量发展的今天&#xff0c;企业对前沿技术和高质量人才的需求不断升级。为了帮助更多开发者、企业洞察行业趋势、技术热点&#xff0c;CSDN 重磅打造技术访谈金牌栏目《架构师说》&#xff0c;聚焦数字化转型、云原生、数据库、开源技术、人工智能、出海…

Nginx 面试 40 连问,快顶不住了~~

欢迎关注方志朋的博客&#xff0c;回复”666“获面试宝典什么是Nginx&#xff1f;Nginx是一个 轻量级/高性能的反向代理Web服务器&#xff0c;用于 HTTP、HTTPS、SMTP、POP3 和 IMAP 协议。他实现非常高效的反向代理、负载平衡&#xff0c;他可以处理2-3万并发连接数&#xff0…

学好计算机第一步

Datawhale干货 作者&#xff1a;cxuan审校&#xff1a;黄元帅前 言计算机已经发展了半个世纪之久&#xff0c;我们从一出生就能享用计算机高速发展的成果&#xff0c;但我们从未对计算机产生过敬畏之心&#xff0c;为什么&#xff1f;因为我们不了解计算机&#xff0c;何谈敬畏…

linux tar的使用方法

tar [-cxtzjvfpPN] 文件与目录 ....参数&#xff1a;-c &#xff1a;建立一个压缩文件的参数指令(create 的意思)&#xff1b;-x &#xff1a;解开一个压缩文件的参数指令&#xff01;-t &#xff1a;查看 tarfile 里面的文件&#xff01;特别注意&#xff0c;在参数的下达中&a…

HDU 4467 分块

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4467 题意&#xff1a;给定n个点m条边的无向图&#xff0c;点被染色(黑0/白1)&#xff0c;边带边权。然后q个询问。询问分为两种&#xff1a; Change u:把点u的颜色反转(黑变白&#xff0c;白变黑)&#xff0c;…

Numpy入门教程:07. 随机抽样

背景 什么是 NumPy 呢&#xff1f; NumPy 这个词来源于两个单词 – Numerical和Python。其是一个功能强大的 Python 库&#xff0c;可以帮助程序员轻松地进行数值计算&#xff0c;通常应用于以下场景&#xff1a; 执行各种数学任务&#xff0c;如&#xff1a;数值积分、微分、…